apache - 如何才能mod_rewrite并保留查询字符串?

标签 apache .htaccess mod-rewrite query-string

我想mod_rewrite一个URL到另一个页面,但然后我还希望保留所有添加的查询字符串。

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

什么样的mod_rewrite规则/条件将使这种情况发生?

最佳答案

您需要添加 [QSA] flag(“查询字符串追加”)

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

对于使用[R]标志的页面301重定向(而不是像这样的内部重写),查询字符串将自动附加。但是,必须使用[QSA]强制进行内部重写。

关于apache - 如何才能mod_rewrite并保留查询字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12873137/

相关文章:

.htaccess - 限制 POST 请求服务器

php - 具有多个参数的 htaccess 重写规则

.htaccess - 将 .htaccess 规则转换为 nginx.conf

.htaccess - mod_rewrite 导致排名下降

python - apache2、mod_wsgi、python 网络应用程序(bottle 框架)

wordpress - <If> 语句破坏了 Rewrite* 规则

php - 个人资料页面有问题

php - 在 session 链接上运行 apache 基准测试。 (我想访问一些正在 session 中的页面。)

java - 我已经安装了两个服务器apache 和Tomcat。我如何删除 http ://example. com :8080 to normal url or http://example. com/folder 或 http ://example. com

git - 使用 git repo 处理 .htaccess 的最佳方法是什么?