0

I need to have all _ (underscores) in every url to be replaced with - (hyphens)

I currently do it this way, but am looking for a more simpler way of doing this so I do not have to add a line every time a url gets longer.

RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)-([^.]+)-([^.]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5_$6_$7_$8_$10 [L]
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)-([^.]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5_$6_$7_$8_$9 [L]
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5_$6_$7_$8 [L]
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5_$6_$7 [L]
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5_$6 [L]
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4_$5 [L]
RewriteRule ^([^_]+)-([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3_$4 [L]
RewriteRule ^([^_]+)-([^_]+)-([^.]+)$ index.php?/$1_$2_$3 [L]
RewriteRule ^([^_]+)-([^.]+)$ index.php?/$1_$2 [L]

Thanks.

cointilt
  • 698
  • 2
  • 8
  • 18

4 Answers4

3

sedhyphen.sh:

#!/bin/sh
sed -u 's/_/-/g'

httpd conf:

RewriteMap sed-hyphen prg:sedhyphen.sh
RewriteRule ^(.*)$ index.php?/${sed-hyphen:$1} [L]

Make sure that sedhyphen.sh is set executable.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

I know this is and old question, but here is my solution. It doesn't waste resources, it's SEO friendly, and it doesn't require you to use a bash, relying instead on php.

Place this in your .htaccess after the rewrite engine has been started

RewriteCond %{REQUEST_URI} (.*)_(.*)
RewriteRule (.*)$ /tools/underscore_to_hyphen.php?rewrite_uri=$1 [NC,L]

This will send all files the php script underscore_to_hyphen.php in which you put this code:

<?php
$path = $_GET['unchanged_path'];
$input_uri = $_GET['rewrite_uri'];

$output_uri = str_replace("_", "-", $input_uri);

//For redirect uncoment:
header("HTTP/1.1 301 Moved Permanently");
header("Location: $path$output_uri");
exit();

//For rewrite uncoment:
//include_once "$_SERVER[DOCUMENT_ROOT]$path$output_uri";
?>

Which will send it on the right place.

Because this is a Rewrite rule without redirection in the .htaccess, it will result in a single redirect or rewrite for the user/spider.

Please note that this is only partially tested and I used it in reverse, to change - to _.

OneHoopyFrood
  • 3,829
  • 3
  • 24
  • 39
0

mod_rewrite is not a good tool for that kind of work. Why don’t you replace with PHP?

$_SERVER['QUERY_STRING'] = str_replace('_', '-', $_SERVER['QUERY_STRING']);

Edit    Well you could try these rule sets:

# using the path
RewriteRule ^([^-]+)-([^-]+-.+) /$1_$2 [N]
RewriteRule ^([^-]+)-(.+) index.php?/$1_$2 [L]

# using the query
RewriteRule ^([^-]+)-(.+) index.php?/$1_$2
RewriteCond %{QUERY_STRING} ^([^-]+)-(.+)
RewriteRule ^index\.php$ ?$1_$2 [N]
RewriteRule ^index\.php$ - [L]

But be careful with the N flag as you can easily get an infinite recursion.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • I am using Codeigniter, so the urls have to have _ in them to reference the controller functions. function new_page() {} can not be function new-page() {}. So the urls just need to show up different in the browser url. – cointilt Jan 20 '10 at 21:23
  • Is that the same as I have above, since you are only have $1 and $2. I have the possibility of having over 10 _ needing to be replaced with - – cointilt Jan 20 '10 at 22:22
  • @Will Ayers: The *N* flag will take care of it and loop as long as the rule is applied. But as I said, mod_rewrite is not the best tool to do such things as using *N* is dangerous. – Gumbo Jan 20 '10 at 23:22
  • Is there a way to set a max to N? or could I replace N with a number like 20? – cointilt Jan 20 '10 at 23:35
  • @Will Ayers: No, rules with *N* will restart the rewrite process everytime the rule is applied. There is no counter that can stop it. That’s the dangerous thing about the *N* flag. But unlike the *L* flag, the *N* flag does not generate an internal redirect that are limited and thus can replace an arbitrary number of hyphens. – Gumbo Jan 20 '10 at 23:56
  • Since Apache 2.4.8, you can set a limit on # of loops, like that `[N=1000]`. – i-- Mar 27 '17 at 20:26
0

Perhaps this:

RewriteCond %{REQUEST_URI} ^(.*)_(.*)/$
RewriteRule (.*)_(.*)/ http://your-site.com$1-$2/ [R=301]