Mohit Bhardwaj
Mohit Bhardwaj

Reputation: 10083

htaccess redirection loop issue

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.

Upvotes: 1

Views: 34

Answers (1)

anubhava
anubhava

Reputation: 784898

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

Upvotes: 1

Related Questions