I have a situation where I need to redirect a URL like this (1)
to a URL like this (2)
In my app, I know that the ID 145 corresponds to the word john. How would I create a 301 RedirectRule which takes (1), finds the value for the ID using a PHP or other script, and then applies the found value i.e. “john” dynamically in (2)?
This solution needs to be computed because there are over 150,000 unique ID’s. If done manually, that would mean 150,000 RedirectRule lines in .htaccess – which doesn’t seem a good plan.
Use a rule like this:
RewriteRule ^person/(\d+)$ person.php?id=$1&redirect=true [L,QSA]and have
person.phpperform a redirect tohttp://site.com/person/john. Your application “knows” how to perform the transformation, so don’t waste time rewriting that logic.If the two URLs are equivalent, then it can be argued there’s minimal benefit in doing this, since the same lookup will need to be applied twice (albeit in different directions), and it slows down the user experience a little. If you’re worried about, e.g. search engines indexing the “uglier” URL, then have the page include a canonical reference, e.g.
<link rel="canonical" href="http://site.com/person/john" />