Forum Moderators: phranque
I'm having a monkey of a time trying to get these 301s to work. I've recently changed the organization of my site and I need to redirect some old URLs to new ones.
Previously I was using:
# Old stuff
RewriteRule ^post/([0-9]+)/$ view-news.php?id=$1
This would create URLs that look like:
http://www.example.com/post/123/
I have since updated my .htaccess to this:
# New News sections
RewriteRule ^news/([0-9]+).html$ /view-news.php?id=$1
So now I am spitting out URLs that look like:
http://www.example.com/news/123.html
My problem is that I have a ton of backlinks to the previous URLs and I want to forward those on to the new ones. Google also has about 200 or the old URLs still indexed and I would like to spit out a 301 to help smooth over the transition.
Please assist as I cannot seem to get this working. Thanks in advance!
This would create URLs that look like:
http://www.example.com/post/123/
In order to clean up the previously-indexed dynamic URLs, you need to use redirects that can tell the difference between a dynamic URL requested by a client (which you do want to redirect) and a dynamic URL occurring as the result of your current internal rewrite rules (which you don't want to redirect, because doing so would cause an 'infinite' rewrite/redirect loop). To do this, you must use the server variable THE_REQUEST, which contains the HTTP request header received from the client, but is unaffected by any internal rewrites.
Here's an example to 'clean up' your first URL:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /view-news\.php\?id=([0-9]+)\ HTTP/
RewriteRule ^view-news\.php$ http://www.example.com/post/%1/ [R=301,L]
Jim