Remove File Extension for Page Outside of WordPress

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.com/apple-app-site-association.html needs to be https://example.com/apple-app-site-association/.

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

1 Answer
1

…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.com/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.

Leave a Comment