Help needed - 301 redirect existing structure to new structure

Discussion in 'Apache' started by StevePro, Aug 16, 2009.

  1. #1
    Hi, I'm struddling with 301 redirects. I have an existing site structure that uses mod rewrite to give me SEO-friendly pages, however the existing structure isn't very nice so I'm re-writing the code to allow for a better structure.

    The problem I have is I don't know how to do 301 redirects for re-writen urls. E.g.:

    I currently have a page like:

    xhttp://www.animal-care.co.uk/cats/abyssinian/breeds/articles/abyssinian-the-facts-every-owner-of-this-cat-breed-should-know.html

    This uses a htaccess file using this rule to re-write the path:

    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/articles/([^/]+).html /articles.php?panimal=$1&animal=$2&articletag=$4 [NC]

    This page with the new code will have a new url of:

    xhttp://www.animal-care.co.uk/articles/abyssinian-the-facts-every-owner-of-this-cat-breed-should-know.html

    using this re-write rule:

    RewriteRule ^articles/([^/]+).html /article.php?article=$1 [NC]

    Note - the xhttp is just to prevent the url's from generating as links for this post

    I need to redirect the old url to the new one. Can I use a redirect rule to redirect the first url without having to write a rule for every page using the first rewrite rule?:
     
    StevePro, Aug 16, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this:

    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/articles/([^/]+).html  http://www.animal-care.co.uk/articles/$4.html [R=301,L]
    
    RewriteRule ^articles/([^/]+).html /article.php?article=$1 [NC]
    Code (markup):
     
    premiumscripts, Aug 16, 2009 IP
  3. StevePro

    StevePro Peon

    Messages:
    106
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks premiumscripts.

    Will using a rewrite have any negative effect on search engines? I keep hering that we need to use a 301 redirect etherwise the current serp results can be lost.... any clarification would be apprecisted
     
    StevePro, Aug 16, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well, as you can see from my code sample, it is using a 301 redirect:

    [R=301,L]
    Code (markup):
    So this means that google will correctly interpret that the pages have moved to a new location and it shouldn't result in a ranking drop (theoretically).
     
    premiumscripts, Aug 16, 2009 IP
  5. StevePro

    StevePro Peon

    Messages:
    106
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ah perfect, I didn't realise you could use R=301 at the end. Thanks again!
     
    StevePro, Aug 16, 2009 IP