2010-01-20 54 views
0

我必须每个URL所有_(下划线)与被替换 - (连字符)现代重写:更换所有下划线(_)与连字符( - )

我目前做这种方式,但我寻找一个更简单的方法来做到这一点,所以我不必每次URL变长时添加一行。

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] 

谢谢。

回答

3

sedhyphen.sh

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

的httpd的conf:

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

确保sedhyphen.sh设置可执行。

+0

你能解释一下我在哪里把代码sedhyphen.sh和代码#/ bin/sh的和sed -u的/ _/-/G'! – cointilt 2010-01-20 21:25:50

+0

sedhyphen.sh为您创建一个单独的shell脚本。它应该放在'$ PATH'的某处。 – 2010-01-20 21:27:01

+0

这个文件可以在服务器上托管,就像使用ftp的regualr文件一样吗?还是我需要使用ssh在服务器上安装它自己? – cointilt 2010-01-20 21:33:01

0

mod_rewrite并不是一种很好的工具。你为什么不用PHP取代?

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

编辑那么你可以尝试这些规则集:

# 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] 

但要小心的ñ标志,你可以很容易地得到一个无限递归。

+0

我正在使用Codeigniter,因此网址必须在其中引用控制器功能。函数new_page(){}不能是new-page(){}函数。因此,这些网址只需要在浏览器网址中显示不同的内容。 – cointilt 2010-01-20 21:23:41

+0

这与我上面的一样,因为你只有$ 1和$ 2。我有可能有超过10个_需要被替换 - – cointilt 2010-01-20 22:22:50

+0

@Will Ayers:只要应用规则,* N *标志将负责处理并循环。但正如我所说的,mod_rewrite并不是最好的工具,因为使用* N *是危险的。 – Gumbo 2010-01-20 23:22:59

0

也许这:

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

虽然这会工作,会导致每下划线一个额外的HTTP交换。 – 2010-01-20 21:28:18

1

我知道这是老问题,但这里是我的解决方案。它不浪费资源,它的搜索引擎优化友好,它不需要你使用bash,而是依靠php

将此放在您的.htaccess后重写引擎已经启动

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

这将发送所有文件php脚本underscore_to_hyphen.php中,你把这个代码:

<?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"; 
?> 

将发送它在正确的地方。

因为这是.htaccess中没有重定向的重写规则,它将导致用户/蜘蛛单个重定向或重写。

请注意,这只是部分测试,我反向使用它,将-更改为_

相关问题