How to make a 301 redirect in .htaccess without it looping on itself ?
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 :
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 :
This code will perform the redirection only once, which is quite handy in our case.