最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

htaccess - Remove File Extension for Page Outside of WordPress

programmeradmin7浏览0评论

I need to add a plaintext page to my server that is outside of WordPress (it can't have any HTML on it). I created the page off of public_html, but I have to add a .html extension to get the page to load or else it goes to a 404. The problem is that I have to have the file extension removed. I have tried adding the code below to the top of the .htaccess file, but it still is not removing the .html.

Example link: .html needs to be /.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

I need to add a plaintext page to my server that is outside of WordPress (it can't have any HTML on it). I created the page off of public_html, but I have to add a .html extension to get the page to load or else it goes to a 404. The problem is that I have to have the file extension removed. I have tried adding the code below to the top of the .htaccess file, but it still is not removing the .html.

Example link: https://example/apple-app-site-association.html needs to be https://example/apple-app-site-association/.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Share Improve this question edited Apr 16, 2020 at 11:55 MrWhite 3,8911 gold badge20 silver badges23 bronze badges asked Apr 14, 2020 at 21:50 tonjaggarttonjaggart 233 bronze badges 2
  • To clarify, you are adding just ONE page outside of WordPress? Your directive is effectively adding .php to "every" URL?? (Also, it's obviously adding .php, not .html, which seems to be the desired extension?) Note that you don't use .htaccess to "remove" the file extension, as you seem to suggest - it should already be removed in your link (which I'm sure is more desirable - otherwise your users will see it). .htaccess is then used to append the extension (to map the URL to the actual filesystem path that handles the request). – MrWhite Commented Apr 14, 2020 at 23:16
  • Yes it is a page with plain text on it for an app. I realize I forgot to change that to .html but that still didn't remove the extension. I need WordPress to ignore this page but be accessible without any file extension. Here is a screenshot of where the file exists currently - link – tonjaggart Commented Apr 16, 2020 at 0:05
Add a comment  | 

1 Answer 1

Reset to default 2

...but that still didn't remove the extension.

To be clear, the extension should already be removed in the HTML anchor/link. The purpose of .htaccess is primarily to rewrite the extensionless URL back to the underlying filesystem path (which contains the file extension).

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Apart from rewriting to .php, not .html (as stated), this is far too generic. You only appear to have 1 URL that needs to be rewritten, but this rewrites everything that doesn't contain a dot - which probably includes all your WordPress URLs as well!

This will also fail if the requested URL ends in a trailing slash - as in your example.

Bit of an aside, but... the preceding condition (that checks that the request does not map to a file) is most probably unnecessary here anyway, since you are only matching requests that do not contain a dot (ie. do not contain a file extension) - it is unlikely that a URL without a "file extension" maps directly to a file on the filesystem. (This would mean you have files without file extensions - which is generally a bad idea.)

So, providing you are already linking to https://example/apple-app-site-association/ (as stated - with a trailing slash) then add the following before the WordPress front-controller:

Options -MultiViews

# Rewrite "/url/" to "/url.html"
RewriteRule ^(apple-app-site-association)/$ $1.html [L]

# BEGIN WordPress
:

The $1 backreference is simply to save repetition.

There is no need to repeat the RewriteEngine directive (since this is already included in the WordPress block below - the order does not matter). Include the Options directive to disable MultiViews if not already.


UPDATE: If the file really is "plaintext" (and "can't have any HTML on it") as you say, then why not use the .txt extension instead of .html? By using .html, Apache will send a mime-type of text/html, as opposed to text/plain (as with a .txt extension), which would seem to be the desired outcome? Browsers also format text/plain documents differently by default using a monospaced font.

发布评论

评论列表(0)

  1. 暂无评论