问题描述
我要mod_rewrite的一个URL到另一个页面,后来我也想添加任何查询字符串是preserved。
I want to mod_rewrite a URL to another page, but then I also want any query strings added to be preserved.
RewriteEngine On
#enforce trailing slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !#
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://localhost/siteroot/$1/ [L,R=301]
RewriteRule ^apps/([A-Za-z0-9-_]+)/?$ index.php&app=$1
因此,如果用户访问应用程序/ APP1 /
,的index.php?应用= APP1
显示。不过,我希望能够以preserve可选的查询字符串,使来访应用程序/ APP1 /?变量= X
返回的index.php ?应用程序= APP1和放大器;变量= X
So if a user visits apps/app1/
, index.php?app=app1
is shown.However, I want to be able to preserve optional query strings, so that visiting apps/app1/?variable=x
returns index.php?app=app1&variable=x
.
什么的mod_rewrite规则/条件会做到这一点?
What mod_rewrite rule/condition would make this happen?
推荐答案
您需要添加的(查询字符串附加)
You need to add the [QSA]
flag ("query string append")
RewriteRule ^apps/([A-Za-z0-9-_]+)/?$ index.php&app=$1 [L,QSA]
有关301页面重定向与 [R]
标记,而不是内部重写像这样的,查询字符串将自动添加。但是,你必须强迫它 [QSA]
的内部重写。
For page 301 redirects with the [R]
flag as opposed to internal rewrites like this one, the query string is automatically appended. However, you must force it with [QSA]
for the internal rewrite.
这篇关于我怎样才能mod_rewrite的,并保持查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!