mod rewite dashes to underscores?

Discussion in 'Apache' started by stuw, Mar 28, 2006.

  1. #1
    I need to redirect
    /something/file_name.php
    to
    /somethingelse/file-name.php

    The key part being changing the underscore to a dash. Is this something I can do with mod_rewrite? Or should I just redirect it to some php script that parses the url and redirects to the new one?
     
    stuw, Mar 28, 2006 IP
  2. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #2
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^something/([^.]+)_([^.]+)\.php$ http://www.domain.com/somethingelse/$1-$2.php [R=301,L]
     
    Nintendo, Mar 28, 2006 IP
  3. stuw

    stuw Peon

    Messages:
    702
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    cheers fella. That's put my on a track to do some testing at least.

    I'm assuming that I can't have one rule that will convert the _ to - regardless of the number of words
    eg a rule that works for
    /something/hello.php
    /something/hello_big.php
    /something/hello_big_boy.php
    /something/hello_king_of_da_wackos.php

    cheers
     
    stuw, Mar 28, 2006 IP
  4. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #4
    Add...

    RewriteRule ^something/([^.]+)_([^.]+)_([^.]+)\.php$ http://www.domain.com/somethingelse/$1-$2-$3.php [R=301,L]
    RewriteRule ^something/([^.]+)_([^.]+)_([^.]+)_([^.]+)\.php$ http://www.domain.com/somethingelse/$1-$2-$3-$4.php [R=301,L]
    RewriteRule ^something/([^.]+)_([^.]+)_([^.]+)_([^.]+)_([^.]+)\.php$ http://www.domain.com/somethingelse/$1-$2-$3-$4-$5.php [R=301,L]
     
    Nintendo, Mar 28, 2006 IP
  5. mincus

    mincus Peon

    Messages:
    63
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Or you could use a rewrite map (this code is just an example and is totally untested, make sure you do some testing and research on this first if you are going to use it, but it's worked perfectly for me for something similar):

    in your .htaccess:
    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^(.*_.*)$
    RewriteRule ^.*\.php$ ${recmap:%1}? [R=301,NC,L]
    Code (markup):
    in your httpd.conf (under your virtual host for the domain):
    RewriteEngine On
    RewriteMap recmap prg:/usr/local/scripts/rewrite_map.pl
    Code (markup):
    in rewrite_map.pl:
    #!/usr/bin/perl
    
    $|=1;
    
    sub fix_url_name {
        my $url_name = shift;            
    
        $url_name =~ s/_/-/g;
    
        return( $url_name );
    }
    
    while(<STDIN>) {
        chomp;
        print fix_url_name( $_ ) . "\n";
    }
    Code (markup):
     
    mincus, Mar 28, 2006 IP
  6. stuw

    stuw Peon

    Messages:
    702
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #6
    i did
    RewriteRule ^something/(.*).php /redirect/script.php?value=$1
    Code (markup):
    and
    /redirect/script.php

    <?php
    $newurl='http://www.somedomain.com/newsomething/'.str_replace('_','-',$_GET['value']).'.php'
    header("HTTP/1.1 301 Moved Permanently");
    header("location: $newurl");
    exit;
    ?>
    Code (markup):
    Works for me, anyone think why this would be bad?
    I don't know the number of _ that need to be turned into - and I hate perl
     
    stuw, Mar 28, 2006 IP