I had this url on my website example.com/foo.php
and I changed it to example.com/foo
by doing this in my htaccess:
RewriteRule ^foo$ foo.php [NC,L]
And it works fine. However I'd like to do a 301 redirect from the old url to the new url. So I added this line in my htaccess:
RewriteRule ^foo.php$ http://example.com/foo [L,NC,R=301]
And I get an infinite loop of redirects... How to fix that?
You can avoid the loop error using THE_REQUEST variable :
RewriteCond %{THE_REQUEST} /foo\.php [NC]
RewriteRule ^foo\.php$ http://example.com/foo [L,R=301]
RewriteRule ^foo$ foo.php [NC,L]
Or if you are on apache 2.4, you can use the END flag
RewriteRule ^foo\.php$ http://example.com/foo [L,R=301]
RewriteRule ^foo$ foo.php [NC,END]