php .htaccess: GET 变量在重写时丢失

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6656413/
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-26 00:55:26  来源:igfitidea点击:

.htaccess: GET variables are lost in rewrite

php.htaccessmod-rewriteapache2

提问by Industrial

Apparently, my .htaccess rewrite eats up all $_GET-variables on my page:

显然,我的 .htaccess 重写吃掉了$_GET我页面上的所有-variables:

When accessing the URL http://192.168.1.1/welcome/test?getvar=trueand running var_dump($_GET)in my index.php file, I get this this output:

当访问 URL http://192.168.1.1/welcome/test?getvar=truevar_dump($_GET)在我的 index.php 文件中运行时,我得到以下输出:

array
'/welcome/test' => string '' (length=0)

So no $_GET-data available and no sign of the getvar-variable from my URL.

所以没有$_GET-data 可用,也没有getvar来自我的 URL的 -变量的迹象。

Here's my .htaccess:

这是我的.htaccess:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/ [L]

What should I change to ensure that my rewrite is working as intended but $_GET-variables still are accessible?

我应该更改什么以确保我的重写按预期工作但$_GET仍然可以访问 -variables?

回答by Kevin Stricker

You need the "QueryString Append" option:

您需要“QueryString Append”选项:

RewriteRule ^(.*)$ index.php?route=/$1 [QSA,L]

RewriteRule ^(.*)$ index.php?route=/$1 [QSA,L]

Edit: Added @DonSeba's contribution, because it is correct.

编辑:添加了@DonSeba 的贡献,因为它是正确的。

回答by DonSeba

minor detail change :

小细节变化:

RewriteRule ^(.*)$ index.php?/ [L]

to

RewriteRule ^(.*)$ index.php?route= [QSA,L]

now all routes will be visible in $_GET["route"]

现在所有路由都将在 $_GET["route"] 中可见

回答by Naftali aka Neal

RewriteRule ^(.*)?(.*)$ index.php?/& [L]

回答by Colin

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^myapi(.*) ./my_real_api_file.php? [QSA]
</IfModule>

Did the trick for me.

对我有用。

You can now request: http://www.mydomain.com/myapi?foo=xy&bar=ab

您现在可以请求:http: //www.mydomain.com/myapi?foo=xy& bar=ab

and will be redirected to: http://www.mydomain.com/my_real_api_file.php?foo=xy&bar=ab

并将被重定向到:http: //www.mydomain.com/my_real_api_file.php?foo=xy&bar= ab

Can be quite usefull to hide your api code.

隐藏您的 api 代码非常有用。