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?
Options +Indexes Options +FollowSymlinks RewriteEngine on RewriteBase / RewriteRule ^something/([^.]+)_([^.]+)\.php$ http://www.domain.com/somethingelse/$1-$2.php [R=301,L]
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
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]
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):
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