301 redirects

Discussion in 'Apache' started by mjewel, Apr 22, 2005.

  1. #1
    I have a site that had used underscores instead of a dash in its mysql database. I have changed the database so all underscores are now a dash.

    Google has about 5,000 pages indexed (using the underscore) and I would like to do a 301 redirect if it is possible. Right now they just get a custom "request not found page".


    All pages are dynamically created in php and urls are processed using mod_rewrite.

    Is there anything like str_replace which would look for any underscore and change it to a dash?

    If not, what about a list of exact "keywords" i.e. "united_states" "purple_widgets" that could be changed to "united-states" "purple-widgets"?

    As all URI's go through mod_rewrite, is there any problem with making all URI requests (even correct new url's with a dash) a 301?
     
    mjewel, Apr 22, 2005 IP
  2. apachehtaccess

    apachehtaccess Guest

    Messages:
    82
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Ok this is not a difficult thing to do with mod_rewrite mjewel, so you are in luck!

    I would suggest attacking the google index problem in 3 ways...
    1. Using mod_rewrite to send a 301 redirect to the page where all _ are converted to -.
    2. Using your sites robots.txt file to tell google not to index any pages with an underscore (_) in it.
    3. Using Googles webmaster tools to verify your new robots.txt file is working and to speed up googles re-indexing.

    1. mod_rewrite (based on code from "Rewrite urls separated by _ underscores to - dashes")
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    
    RewriteRule !\.(html|php)$ - [S=5]
    RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6-$7 [E=underscores:Yes]
    RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6 [E=underscores:Yes]
    RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [E=underscores:Yes]
    RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [E=underscores:Yes]
    RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [E=underscores:Yes]
    RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=underscores:Yes]
    
    RewriteCond %{ENV:underscores} ^Yes$
    RewriteRule (.*) http://domain.com/$1 [R=301,L]
    Code (markup):
    2. robots.txt (based on code from "SEO with Robots.txt")
    User-agent: Googlebot
    # disallow the indexing of files with an underscore _ anywhere in url
    Disallow: /*_*
    Code (markup):
     
    apachehtaccess, Feb 17, 2007 IP
    mjewel likes this.