Convert path to one get variable

倖福魔咒の 提交于 2019-12-11 10:59:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!