php .htaccess RewriteRule 保留 GET URL 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4071155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:51:45  来源:igfitidea点击:

.htaccess RewriteRule to preserve GET URL parameters

php.htaccessparametersrewrite

提问by Enkay

I'm having issues keeping the parameters of the url working after an htaccess url rewrite.

在 htaccess url 重写后,我在保持 url 的参数工作时遇到问题。

My htaccess rewrite is as follows:

我的 htaccess 重写如下:

 RewriteEngine on
 RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?lang=&page=

Which means:

意思是:

domain.com/index.php?lang=en&page=productdisplays as domain.com/en/product

domain.com/index.php?lang=en&page=product显示为 domain.com/en/product

For some reason, when I add a ?model=AB123&color=somethingat the end of my URLs I am not able to retrieve those parameters in php using $_GET['model']and $_GET['color']even though they are present in the displayed URL.

出于某种原因,当我?model=AB123&color=something在 URL 末尾添加 a时$_GET['model']$_GET['color']即使它们存在于显示的 URL 中,我也无法使用 php 检索这些参数。

Why aren't the variables passed along?

为什么不传递变量?

回答by Simon

You need to append with the [QSA] (query string append) tag. Try

您需要附加 [QSA](查询字符串附加)标签。尝试

RewriteEngine on
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?lang=&page= [QSA]

See http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html