Reputation: 641
I'm trying to remove .html extensions from my urls using this code in .htaccess (code found online).
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
It is not working. I found a post saying to use this to test if mod-rewrite is enabled: /usr/local/apache/bin/httpd -D DUMP_MODULES | grep rewrite
It returns:
Syntax OK
rewrite_module (static)
Nothing in the error log. This is on a server with WHM and cPanel if it matters. My other .htaccess directives are working.
Upvotes: 1
Views: 381
Reputation: 143956
If you make a request for an existing html file, these conditions are going to prevent the rule from doing anything:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Because they say, if the requested file doesn't exist and isn't a directory... And when you request an html file, it's going to fail the -f
test. So rule does nothing.
You want to match against something like the actual request:
RewriteCond %{THE_REQUEST} \ /([^.]+)\.html($|\ |\?)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ /%1 [L,R=301]
This redirects a request for an existing HTML file to the same path but minus the extension. You can then rewrite it back internally:
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ /$1.html [L]
Upvotes: 0
Reputation:
Your code does the exact opposite of what you are trying to accomplish. The correct code would be:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Upvotes: 1