如何使用 mod_rewrite 并保留查询字符串? [英] How can I mod_rewrite and keep query strings?

查看:30
本文介绍了如何使用 mod_rewrite 并保留查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 URL mod_rewrite 到另一个页面,但我也希望保留添加的所有查询字符串.

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

因此,如果用户访问 apps/app1/,则会显示 index.php?app=app1.但是,我希望能够保留可选的查询字符串,以便访问 apps/app1/?variable=x 返回 index.php?app=app1&variable=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?

推荐答案

您需要添加[QSA] 标志(查询字符串追加")

You need to add the [QSA] flag ("query string append")

RewriteRule ^apps/([A-Za-z0-9-_]+)/?$ index.php&app=$1 [L,QSA]

对于带有 [R] 标志的页面 301 重定向,而不是像这样的内部重写,会自动附加查询字符串.但是,您必须使用 [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 并保留查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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