.htaccess rewriterule 保留参数

use*_*788 1 php apache .htaccess mod-rewrite get

我目前在我的 .htaccess 文件中有这一行:

RewriteRule ^(20\d\d)/([A-Za-z0-9-]+)/$ /test/?type=post&year=$1&title=$2 [L]
Run Code Online (Sandbox Code Playgroud)

它允许这样的 URL:

/test/?type=post&year=2014&title=hello
Run Code Online (Sandbox Code Playgroud)

可以这样访问:

/test/2014/hello/
Run Code Online (Sandbox Code Playgroud)

但是,有时我在重写的 URL 上甚至还有另一个 GET 变量:

/test/2014/hello/?page=2
Run Code Online (Sandbox Code Playgroud)

但是,page页面上的 PHP 文件不会读取get 变量。换句话说,如何更改我的 .htaccess 文件以允许 PHP 文件使用和读取其他 get 变量(添加到重写的 URL 中)?

edi*_*999 5

您需要在重写规则中添加一个 QSA 标志,以便括号看起来像这样:

[L,QSA]
Run Code Online (Sandbox Code Playgroud)

这告诉 apache 将任何现有的查询字符串附加到目标 (mode=allBrands) 中的新查询字符串。

更多信息:https : //cwiki.apache.org/confluence/display/HTTPD/RewriteFlags+QSA