Challenge: 301 redirect all _ to -

Discussion in 'Apache' started by djstreet, Jul 27, 2006.

  1. #1
    I've tried for a month, numerous items, different strings, PHP, etc. nothing, NOTHING! I need help please PLEASE... The bots keep wanting to look at my forums old _ in the link and not the new -.




    Context:

    I switch my forum from underscores to hyphens. Trouble is, I can't get the htaccess to 301 redirect so it now looks like I have duplicate content.

    Here's what I've done:

    Redirect 301 /forum/Buy_Sell_Trade_Forums_C15/ http://www.discusswireless.com/forum/Buy-Sell-Trade-Forums-C15/

    multiplied by all the different categories I have. This worked, however, it only worked for that directory and did not apply to forum categories and posts underneath it.

    Another problem is the length of the posts will vary posts by post and forum by forum; I need a more efficient method of changing ALL _ to -

    I've tried this piece:

    RewriteRule ^forum/([^_]*)_([^_]*)_([^_]*)/([^_]*)_([^_]*)_([^_]*)/([^_]*)_([^_]*)_([^_]*)_([^_]*)/$ /forum/$1-$2-$3/$4-$5-$6/$7-$8-$9-$10/ [L,R=301]

    But that's not working.....I'm looking for something to change everything in one swoop.

    I could poetntially have links this long:

    http://www.mysite.com/forum/Discuss-Cell-Phones-C1/Kyocera-Forum-F34/Kyocera-unveil-the-Strobe-P2523/

    (of course with underscores)


    Any htaccess gurus out there? I also tried this: RewriteRule ^/([-a-zA-Z0-9/]+)_(.+)$ /$1-$2 [L]

    But that didin't work.....

    ANd I've tried calling a script.php from htaccess in attempts to get it to work:

    Code:

    RewriteRule ^/forum/.+_.+ script.php



    script.php:
    Code:

    $newUrl = str_replace('_', '-', $_SERVER["REQUEST_URI"]);
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: ' . $newUrl);




    Any thoughts at all? Someone must know, even a 'that's impossible :p" thanks.
    Thanks in advance
     
    djstreet, Jul 27, 2006 IP
  2. Cryogenius

    Cryogenius Peon

    Messages:
    1,280
    Likes Received:
    118
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Perhaps you could do it in PHP code instead of via .htaccess. Scan the requested URL for underscores, and if you find any replace them with dashes and respond with a 301 redirect. You'll find this much more flexible than mod_rewrite. Here's some (untested) code that might be in the right direction:

    if (strpos($_SERVER['PHP_SELF'], '_')) {
        $filename = str_replace('_', '-');
        header('HTTP/1.1 301 Moved Permanently');
        header("Location: http://www_domain_com" . $filename);
        exit;
    }
    PHP:
    You might need to replace PHP_SELF with a different variable, and you may need something more sophisticated than strpos(). I do something similar on my own web site, and I find it can be very effective.

    Hope this helps, Cryo.
     
    Cryogenius, Jul 28, 2006 IP
  3. djstreet

    djstreet Peon

    Messages:
    52
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Could someone comment on 301 redirects via htaccess and PHP? I've heard I can do appropriate redirects via PHP (through htaccess) but Google (et al) will KNOW that I've changed the link and will penalise/filter/etc. me for doing so. Therefore it would be better to change in mod_rewrite.
     
    djstreet, Jul 28, 2006 IP
  4. Cryogenius

    Cryogenius Peon

    Messages:
    1,280
    Likes Received:
    118
    Best Answers:
    0
    Trophy Points:
    0
    #4
    As far as googlebot can tell, the 301 redirect from either PHP or .htaccess will look the same. They won't penalise you for a 301 permanent redirect, because by definition it is has been moved, not duplicated. After a while, search engines will stop indexing your old URLs in preference for your new ones.

    A 302 temporary redirect, on the otherhand, is seen as a bad thing by search engines.

    If you use mod_rewrite to serve both "some_kind_of_script.php" as well as "some-kind-of-script.php", then they may consider this as duplicate content.

    Cryo.
     
    Cryogenius, Jul 28, 2006 IP
  5. djstreet

    djstreet Peon

    Messages:
    52
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5

    As far as PHP I'm green. what would I replace PHP_SELF with? and whats' more sphisticated than strpos()?

    I put the script.php code you suggested into a folder and called up a link with an _ but it still called the _ file and did not redirect...... Options, thoughts? anybody!? tX
     
    djstreet, Jul 28, 2006 IP
  6. Cryogenius

    Cryogenius Peon

    Messages:
    1,280
    Likes Received:
    118
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Reading through my code, I spotted the mistake I made on line 2 - here's the corrected version:

    if (strpos($_SERVER['PHP_SELF'], '_')) {
        $filename = str_replace('_', '-', $_SERVER['PHP_SELF']);
        header('HTTP/1.1 301 Moved Permanently');
        header("Location: http://www_domain_com" . $filename);
        exit;
    }
    PHP:
    Otherwise, I think this should work...
     
    Cryogenius, Jul 28, 2006 IP
  7. djstreet

    djstreet Peon

    Messages:
    52
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Well I used your suggestion and unfortuantely it still won't work.

    I'm testing this in a sub folder and calling a fiel:

    www.yoursite.com/folder/abc_def.html

    I want it to display the other file:

    www.yoursite.com/folder/abc-def.html within the browser.

    Still no luck. Have you managed to do a fix like this? Thanks for your hlep.
     
    djstreet, Jul 28, 2006 IP
  8. Cryogenius

    Cryogenius Peon

    Messages:
    1,280
    Likes Received:
    118
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I've given this some more thought, and come up with version 3 of that code, (this time I have tested it):
    if (strpos($_SERVER['REQUEST_URI'], '_')) {
        $filename = str_replace('_', '-', $_SERVER['REQUEST_URI']);
        header('HTTP/1.1 301 Moved Permanently');
        header("Location: http://" . $_SERVER['SERVER_NAME'] . $filename);
        exit;
      }
    PHP:
    Now, this will only redirects "www.yoursite.com/some_folder/abc_def.php" to "www.yoursite.com/some-folder/abc-def.php".
    If you want to redirect .html files you need to add some RewiteRules to your .htaccess so that the PHP gets parsed. You may already have these if your html is actually PHP output. You could integrate this code into your existing PHP code...

    Don't for get to remove your redirects from your .htaccess file.

    Good luck, Cryo.
     
    Cryogenius, Jul 30, 2006 IP
  9. djstreet

    djstreet Peon

    Messages:
    52
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks for the help, the learning curve is pretty high for me righ tnow so bear with me. I run a perl script that calls off the sQL database; therefore the forum is not php.


    why would i have to remove my redirect in htaccess? or u referring to the old ones? thanks again for the help.
     
    djstreet, Jul 30, 2006 IP
  10. djstreet

    djstreet Peon

    Messages:
    52
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    This wokred for me, any thoughts on it/ Any glaring errors?

    Thanks.



    Code:
    Options +FollowSymLinks

    RewriteEngine On

    RewriteRule ^forum/.*_.*$ script.php [L]


    script.php:
    Code:
    <?php

    header('Location: http://' . getenv('HTTP_HOST')
    . str_replace('_', '-', getenv('REQUEST_URI')), true, 301);
     
    djstreet, Aug 8, 2006 IP
  11. Cryogenius

    Cryogenius Peon

    Messages:
    1,280
    Likes Received:
    118
    Best Answers:
    0
    Trophy Points:
    0
    #11
    If it works, then it's probably okay. I can't see anything wrong with it. Well done for figuring it out, looks like a nice simple solution.

    Cryo.
     
    Cryogenius, Aug 8, 2006 IP