jamiestroud69
jamiestroud69

Reputation: 277

htaccess Removed File Extension Yet Extension Still Exists

Using htaccess, I was able to make my html urls function without a file extension, yet the file extension still exists. For example:

example.com/index AND example.com/index.html BOTH exist!

I ONLY want my html file urls to exist at the single stripped url (e.g. example.com/index )

I've tried manually deleting the .html off of my files, but then the files will simply load as plain text. What can I do to achieve the results I want?

htaccess code:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.html

Upvotes: 2

Views: 66

Answers (1)

Panama Jack
Panama Jack

Reputation: 24448

Rewrite doesn't actually remove the extension. It just allows you to not use it and internally rewrites to it in the rule. Also you can't manually make the file extension disappear. It has to be there for it to work. So in addition to your current rule you need to check the request and redirect to the extensionless URL if they enter .html on it. Use this rule and let me know how it works for you.

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^&\ ]+).html
RewriteRule .* /%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.html [L]

Upvotes: 1

Related Questions