开发者

Mod Rewrite : Replace all _ (underscore) with - (hyphen)

开发者 https://www.devze.com 2022-12-16 21:19 出处:网络
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 ti

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.


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.


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 _.


Perhaps this:

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消