php - $_GET 变量在 mod_rewrite 将 .html 重写为 .php 时消失

标签 php apache .htaccess mod-rewrite url-rewriting

我正在使用 mod_rewrite 将 .html 重写为 .php。

RewriteRule ^([0-9a-zA-Z-_]+)\.html$ index.php?p=$1 [L]

但是,我在 URL 中使用 $_GET 时遇到了问题。如果请求的 URL 是这种格式,它工作正常:index.php?a=f1

但是,如果将其重写为 home.html?a=f1,则什么都没有。

假设它只是浏览器在点击 .html 后停止阅读。有什么办法可以做到这一点吗?

最佳答案

您需要添加 QSA标志:

RewriteRule ^([0-9a-zA-Z-_]+)\.html$ index.php?p=$1 [QSA,L]

原因最好在文档中解释:

When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

关于php - $_GET 变量在 mod_rewrite 将 .html 重写为 .php 时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11234188/

相关文章:

linux - 我的主目录与 "/var/www/"Apache Linux 相同

Apache 重定向 - 不区分大小写

php - htaccess 文件遇到错误

java - apache上传文件大小覆盖org.apache.commons.fileupload.FileUploadBase

php - Laravel Dusk - 类配置不存在

php - 上传并存储图像到服务器时为 0 kb

javascript - 在 HTML 选择中复制数据(选项)

python - 使用不同的 python 版本运行多个 CherryPy Web 应用程序

apache - .htaccess 错误的 HTML 500 错误页面

php - 如何在 SQL 中管理产品选项和仓库项目之间的关系