Combine two Mod Rewrite Rules
Current Status: Firstly, I would like to point out that this question is very much related to a question which I posted two days ago. I have since tried to tackle the problem I was facing a different way and have encountered different problems. I hope this does not constitute any sort of "double-post"?
I have created two mod rewrite rules which seem to work in isolation to create the desired effect. These are shown below:
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*/)?([A-Za-z0-9.-]+)_([A-Za-z0-9.-]*) $1?$2=$3&%1 [L]
The above rule takes a URL like this:
www.site.com/param1/thing/sortorder_5/getparam2_6/
and converts it into this:
www.site.com/param1/thing/?sortorder=5&getparam2_6/
The second rule is as follows:
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*/)?((?:cmd)[^/]*)/((?!(?:cmd)[.+]*)(.+)) $1?$2=$3&%1 [L]
This takes a URL like this:
http://www.site.com/cmd/param1/thing/cmd2/param2/
and converts it into this:
http://www.site.com?cmd=param1/thing&cmd2=param/
Desired Outcome:
I would like to merge these two rewrite rules so that I may be able to do the following:
http://www.site.com/cmd/param1/cmd2/param2/sortorder_5
converts to:
http://www.site.com?cmd=param1&cmd2=param2&sortorder=5
Anybody who tells me how to begin doing so would be most kind. I have experimented with LOTS of mod rewrite code for days, with no luck. My regex/mod rewrite knowledge really isn't up to scratch, so my apologies for that! The whole mod rewrite code is pasted below. Thank you.
RewriteEngine On
# This block works fine in isolation
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*/)?([A-Za-z0-9.-]+)_([A-Za-开发者_JS百科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]
This is tested and works:
RewriteEngine On
RewriteRule ^(cmd[^/]*)/([^/]+)/?(.*)$ $3?$1=$2 [QSA,NC,L]
RewriteRule ^([a-z\d.-]+)_([a-z\d.-]+)/?(.*)$ $3?$1=$2 [QSA,NC,L]
See mod_rewrite docs about the flags.
Example request:
http://localhost:8080/test/cmd1/foo/cmd2/bar/foo_bar/cmd42/everything
Ends up at:
http://localhost:8080/test/index.pl?cmd42=everything&foo=bar&cmd2=bar&cmd1=foo
With the .htaccess
located at /test/
, and index.pl
printing its query string:
cmd42=everything&foo=bar&cmd2=bar&cmd1=foo
This request has the redirect log:
[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
This log can be seen by adding this in your server config:
RewriteLog "logs/rewrite.log"
RewriteLogLevel 2
精彩评论