1

I have a static website that has several html pages. In order to not hide .html extensions from url, we have following rules in htaccess for each url:

RewriteRule ^website-development$               website-development.html

This works fine in that if you open site.com/website-development, it actually opens the website-development.html file but in url it does not show the .html extension.

Now, we need to redirect(or hide) .html urls to their corresponding urls without .html e.g. if someone opens site.com/website-development.html in their browser, the url should be shown as site.com/website-development.
To do this, I added following rule:

 RewriteRule ^(.*)\.html$ /$1 [L,R]  

But doing this results in indefinite redirection and so the browser just keeps redirecting and never renders the actual page. Can you please suggest how I can redirect both site.com/website-development and site.com/website-development.html to site.com/website-development (which actually is an html file - website-development.html ). Thanks.

1 Answer 1

1

Yes indeed, this rule will cause a redirect loop:

RewriteRule ^(.*)\.html$ /$1 [L,R]

It is due to the fact your REQUEST_URI is adding and removing .html in 2 different rules and mod_rewrite runs in a loop.

You can use this rule instead based on a RewriteCond that uses THE_REQUEST

RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules. Example value of this variable is GET /index.php?id=123 HTTP/1.1

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Let me try it once.
It worked exactly as required and solved my problem. Thanks a lot :) . I will read more about RewriteCond and THE_REQUEST to gain more understanding. Also, I have read at some places to not use R=301 while I am doing testing, is that right?
Yes that's correct. While testing keep it R=302 or just R to avoid browser caching issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.