Hi there I have a site with a load of php extensions that I would like to make .html. I have looked for a solution online and found this : RewriteBase / RewriteRule ^(.*)\.php $1.html [R=301,L] RewriteRule ^(.*)\.html $1.php [L] Code (markup): However this results in a redirect loop. Is there a simple way I can rewrite the .php to .html extensions and 301 the old to the new? Thanks
Ah sorry, I misunderstood what you wanted to do last time. I think this might do the trick. RewriteEngine On RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteRule ^(.*)\.php$ /$1.html [R=301,L] RewriteRule ^(.*)\.html$ /$1.php [L] Code (markup):
You are a genius - this is perfect. Can I ask what the ENV:REDIRECT_STATUS actually does so I can understand it?
The problem before was when you requested say test.html, Apache would process the .htaccess file and make a sub request for test.php. At this point .htaccess is processed again, and because test.php matches the rewrite rule, it's redirected to test.html, and the loop will start again! REDIRECT_STATUS is a server variable. If a sub request is made, this is the HTTP response status code of the original request, otherwise it'll be empty. Therefore you can test against this to avoid a loop. This condition is checking that REDIRECT_STATUS is empty: RewriteCond %{ENV:REDIRECT_STATUS} ^$ Code (markup): I hope this helps somewhat.