将路径转换为一送变量 [英] Convert path to one get variable

查看:86
本文介绍了将路径转换为一送变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何去改变这个网址:

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

http://example.com/this/is/my/path.html http://example.com/this/is/my/path.html?var=2&varr=3

这样:

http://example.com?path=this/is/my/path.html http://example.com?path=this/is/my/path.html&var=2&varr=3

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.

推荐答案

请参阅下面

试试这个:

RewriteEngine On

RewriteRule ^(.*)$ ?path=$1 [QSA]

的重写规则的第一部分将捕获而不查询字符串(GET参数)的完整路径。该重写规则的第二部分将改写名为包含我们之前所拍摄的全路径路径GET参数。然后,使用[QSA]标志(这应该是阿帕奇的默认行为),我们告诉Apache将保持原有的GET参数,然后附加到新的查询字符串。

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.

编辑:

没有与该答案,重定向循环的一个主要问题。在这里,一个更好的解决方案:

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]

这篇关于将路径转换为一送变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆