Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm using the .htaccess below to remove the .html file extension but if someone types in

example.com/test

into the browser it redirects to

http://www.example.com/test.html

.

Why does the .html gets added and not removed?

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html 
share|improve this question

1 Answer 1

The line doing the substitution is

RewriteRule ^([^/]+)/$ $1.html 

which will unconditionally add .html to any request where the only slash after the hostname is the last character (e.g. http://example.com/test/, but not http://example.com/test1/test2/).

If you want to strip .html, try this:

RewriteRule (.*)\.html$ $1
share|improve this answer
    
Try it and it does not remove the .html. It's the same as before. RewriteEngine on RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.html -f RewriteRule (.*)\.html$ $1 What else could I try? –  Tom Apr 13 '13 at 13:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.