.htaccess reverse rewrite without looping?
-
06-07-2019 - |
Question
I have an existing page called bfly.php that I need to change to butterfly-jewelry.php
I want to make it so that if someone goes to the URL butterfly-jewelry.php they get the bfly.php page but the url doesn't change it stays as butterfly-jewelry.php. BUT also if someone goes directly to bfly.php the url changes to butterfly-jewelry.php
Is there a way to do this without getting stuck in a rewrite loop?
Thanks!
Solution
You could rename the file to e.g. bfly1.php and then use this:
RewriteRule ^butterfly-jewelry\.php bfly1.php [L,QSA]
RewriteRule ^bfly\.php http://yourhost.com/butterfly-jewelry.php [L,QSA,R=301]
(I tried Blixt's solution, but it caused an infinite loop despite the L-flag.)
OTHER TIPS
You can use the Last Rule and Redirect flags to do what you want (see the mod_rewrite documentation):
# Rewrite request to bfly.php, and then stop the rewrite engine.
RewriteRule ^butterfly-jewelry\.php$ bfly.php [L]
# Redirect the client to butterfly-jewelry.php with "Permanently Moved" status
RewriteRule ^bfly\.php$ butterfly-jewelry.php [L,R=301]
I haven't tested the above, but I believe it should work.