A looping redirection problem sometimes occurs when trying to do a 301 redirect from a non-rewritten page to a rewritten page.
Exemple :
I have a page whose URL is www.monsite.ndd/ma_page.php
I want to rewrite it to www.monsite.ndd/une-url-propre-pour-ma-page
So, I write in my .htaccess file :
RewriteRule ^une-url-propre-pour-ma-page$ /ma_page.php
However, a problem arises : the URL /my_page.php is still accessible. .
I would like that if the user types www.monsite.ndd/ma_page.php they are automatically redirected to : www.monsite.ndd/une-url-propre-pour-ma-page.
But if we write a rule that sends ma_page.php to /une-url-propre-pour-ma-page and we leave the above rule…
Here we are with a beautiful infinite loop : S
Here is a solution :
RewriteRule ^une-url-propre-pour-ma-page$ /ma_page.php [L,E=NOM_DE_MA_VAR:1]
RewriteCond %{ENV:REDIRECT_NOM_DE_MA_VAR} !=1
RewriteRule ^ma_page.php /une-url-propre-pour-ma-page [R=301]
This code will perform the redirection only once, which is quite handy in our case.