2011-07-19 72 views
1

当前状态: 首先,我想指出这个问题与我前两天发布的问题有很大关系。自那时以来,我一直试图解决我遇到的问题,并遇到了不同的问题。我希望这不构成任何“双重职位”?结合两条Mod重写规则

我已经创建了两个似乎独立工作的mod重写规则,以创建所需的效果。这些如下:

RewriteCond %{QUERY_STRING} (.*) 
RewriteRule ^(.*/)?([A-Za-z0-9.-]+)_([A-Za-z0-9.-]*) $1?$2=$3&%1 [L] 

上述规则从这样一个URL:

www.site.com/param1/thing/sortorder_5/getparam2_6/ 

并将其转换成这样:

www.site.com/param1/thing/?sortorder=5&getparam2_6/ 

第二规则如下:

RewriteCond %{QUERY_STRING} (.*) 
RewriteRule ^(.*/)?((?:cmd)[^/]*)/((?!(?:cmd)[.+]*)(.+)) $1?$2=$3&%1 [L] 

这需要一个这样的URL:

http://www.site.com/cmd/param1/thing/cmd2/param2/ 

并将其转换成这样:

http://www.site.com?cmd=param1/thing&cmd2=param/ 

期望的结果:

我想合并这两个重写规则,所以我可能能够做到以下几点:

http://www.site.com/cmd/param1/cmd2/param2/sortorder_5 

转换为:

http://www.site.com?cmd=param1&cmd2=param2&sortorder=5 

任何告诉我如何开始这样做的人会是最善良的。我已经尝试了很多天的mod重写代码,没有运气。我的正则表达式/ mod重写知识确实没有达到要求,所以我很抱歉!整个mod重写代码粘贴在下面。谢谢。

RewriteEngine On 

# This block works fine in isolation 
RewriteCond %{QUERY_STRING} (.*) 
RewriteRule ^(.*/)?([A-Za-z0-9.-]+)_([A-Za-z0-9.-]*) $1?$2=$3&%1 [L] 

# This block also works fine in isolation 
RewriteCond %{QUERY_STRING} (.*) 
RewriteRule ^(.*/)?((?:cmd)[^/]*)/((?!(?:cmd)[.+]*)(.+)) $1?$2=$3&%1 [L] 

# This block should append the query string to index.php (front controller) 
RewriteCond %{QUERY_STRING} ^(.*)$ 
RewriteRule ^.*$ index.php?%1 [L] 
+0

如果你有过'index.php'我建议只是在做类似的东西'重写规则^(。*)$控制?query = $ 1 [L,QSA]'并解析脚本中的查询。这样,除了更容易地解析请求之外,您不依赖于mod_rewrite规则(可以轻松地与其他服务器一起使用)。 – Qtax

+0

顺便说一句,你为什么使用'RewriteCond%{QUERY_STRING}(。*)'那样?我建议在你的重写规则中使用'QSA'标志。 – Qtax

回答

0

这是测试和工程:

RewriteEngine On 
RewriteRule ^(cmd[^/]*)/([^/]+)/?(.*)$ $3?$1=$2 [QSA,NC,L] 
RewriteRule ^([a-z\d.-]+)_([a-z\d.-]+)/?(.*)$ $3?$1=$2 [QSA,NC,L] 

mod_rewrite docs有关的标志。

实施例请求:

http://localhost:8080/test/cmd1/foo/cmd2/bar/foo_bar/cmd42/everything 

在结束:

http://localhost:8080/test/index.pl?cmd42=everything&foo=bar&cmd2=bar&cmd1=foo 

与位于/test/.htaccess,和index.pl印刷其查询字符串:

cmd42=everything&foo=bar&cmd2=bar&cmd1=foo 

该请求具有重定向日志:

[rid#2f0b590/initial] (2) [perdir /root_http/test/] rewrite 'cmd1/foo/cmd2/bar/foo_bar/cmd42/everything' -> 'cmd2/bar/foo_bar/cmd42/everything?cmd1=foo' 
[rid#2f0b590/initial] (2) [perdir /root_http/test/] strip document_root prefix: /root_http/test/cmd2/bar/foo_bar/cmd42/everything -> /test/cmd2/bar/foo_bar/cmd42/everything 
[rid#2f0b590/initial] (1) [perdir /root_http/test/] internal redirect with /test/cmd2/bar/foo_bar/cmd42/everything [INTERNAL REDIRECT] 
[rid#10dec40/initial/redir#1] (2) [perdir /root_http/test/] rewrite 'cmd2/bar/foo_bar/cmd42/everything' -> 'foo_bar/cmd42/everything?cmd2=bar' 
[rid#10dec40/initial/redir#1] (2) [perdir /root_http/test/] strip document_root prefix: /root_http/test/foo_bar/cmd42/everything -> /test/foo_bar/cmd42/everything 
[rid#10dec40/initial/redir#1] (1) [perdir /root_http/test/] internal redirect with /test/foo_bar/cmd42/everything [INTERNAL REDIRECT] 
[rid#2e16d90/initial/redir#2] (2) [perdir /root_http/test/] rewrite 'foo_bar/cmd42/everything' -> 'cmd42/everything?foo=bar' 
[rid#2e16d90/initial/redir#2] (2) [perdir /root_http/test/] strip document_root prefix: /root_http/test/cmd42/everything -> /test/cmd42/everything 
[rid#2e16d90/initial/redir#2] (1) [perdir /root_http/test/] internal redirect with /test/cmd42/everything [INTERNAL REDIRECT] 
[rid#2f0d558/initial/redir#3] (2) [perdir /root_http/test/] rewrite 'cmd42/everything' -> '?cmd42=everything' 
[rid#2f0d558/initial/redir#3] (2) [perdir /root_http/test/] strip document_root prefix: /root_http/test/ -> /test/ 
[rid#2f0d558/initial/redir#3] (1) [perdir /root_http/test/] internal redirect with /test/ [INTERNAL REDIRECT] 
[rid#2f0eac8/initial/redir#4] (1) [perdir /root_http/test/] pass through /root_http/test/ 
[rid#10c98b0/subreq] (1) [perdir /root_http/test/] pass through /root_http/test/index.html 
[rid#10c98b0/subreq] (1) [perdir /root_http/test/] pass through /root_http/test/index.shtml 
[rid#10c98b0/subreq] (1) [perdir /root_http/test/] pass through /root_http/test/index.cgi 
[rid#10c98b0/subreq] (1) [perdir /root_http/test/] pass through /root_http/test/index.fcgi 
[rid#10c98b0/subreq] (1) [perdir /root_http/test/] pass through /root_http/test/index.pl 

此日志可以通过在你的服务器配置添加此可以看出:

RewriteLog "logs/rewrite.log" 
RewriteLogLevel 2 
+0

不幸的是,答案不幸,但非常感谢你的答复。我喜欢你的建议,简单地将整个URL附加到index.php并以这种方式解析它;更直接。 我很好奇,但是,这可能会如何让它在更多的服务器上使用?当然,在服务器配置中必须启用mod重写功能才能使单个RewriteRule生效? – Pete171

+0

其他服务器可以有其他方式将这些数据传递给脚本。你可以在config或其他类型的重写规则中设置它。 – Qtax

+0

@ Pete171,更新并更正了解决方案。 – Qtax