当前状态: 首先,我想指出,这个问题与我两天前发布的问题非常相关。从那时起,我试图解决我遇到的问题,并遇到了不同的问题。我希望这不构成任何形式的“双重发帖”?
我创建了两个mod重写规则,它们似乎是孤立地工作以创建所需的效果。这些如下所示:
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*/)?([A-Za-z0-9.-]+)_([A-Za-z0-9.-]*) $1?$2=$3&%1 [L]
以上规则采用如下网址:
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]
这需要这样的网址:
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 :(得分: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
.htaccess
位于/test/
,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