问题
How would I go about transforming this URL:
http://example.com/this/is/my/path.html
http://example.com/this/is/my/path.html?var=2&varr=3
to this:
http://example.com?path=this/is/my/path.html
http://example.com?path=this/is/my/path.html&var=2&varr=3
I've seen many tutorials on how to convert it to multiple get variables but none for the whole path that included the get variables as well.
回答1:
See my edit below
Try this :
RewriteEngine On
RewriteRule ^(.*)$ ?path=$1 [QSA]
The first part of the RewriteRule will capture the full path without the query string (GET parameters). The second part of the RewriteRule will rewrite with a GET parameter named "path" containing the full path we captured before. And then, using the [QSA] flag (which should be default behaviour of Apache), we tell Apache to keep the original GET parameters and append then to the new query string.
EDIT :
There is a major problem with this answer, a redirection loop. Here a better solution :
RewriteEngine On
RewriteCond %{QUERY_STRING} !path=
RewriteRule ^(.*)$ ?path=$1 [QSA,L]
来源:https://stackoverflow.com/questions/23329488/convert-path-to-one-get-variable