Forum Moderators: phranque

Message Too Old, No Replies

301 Redirect

         

madk

1:17 pm on Aug 17, 2007 (gmt 0)

10+ Year Member



Hello all,

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!

jdMorgan

2:51 pm on Aug 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For the sake of clarity, we need to address this statement:

This would create URLs that look like:
http://www.example.com/post/123/

mod_rewrite code does not create URLs. Rather, it 'maps' the URLs already created by the links published on your Web pages to the internal filepath on the server necessary to serve content for those requested URLs.

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]

You can modify this as needed to 'clean up' all the URL variations you've used.

Jim