简体   繁体   English

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

[英].htaccess: GET variables are lost in rewrite

Apparently, my .htaccess rewrite eats up all $_GET -variables on my page:显然,我的 .htaccess 重写占用了我页面上的所有$_GET -变量:

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

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

So no $_GET -data available and no sign of the getvar -variable from my URL.所以没有可用的$_GET数据,也没有来自我的 URL 的getvar变量的迹象。

Here's my .htaccess:这是我的 .htaccess:

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

What should I change to ensure that my rewrite is working as intended but $_GET -variables still are accessible?我应该改变什么以确保我的重写按预期工作但$_GET -变量仍然可以访问?

You need the "QueryString Append" option:您需要“QueryString Append”选项:

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

Edit: Added @DonSeba's contribution, because it is correct.编辑:添加了@DonSeba 的贡献,因为它是正确的。

minor detail change:小细节改动:

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

to

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

now all routes will be visible in $_GET["route"]现在所有路线都将在 $_GET["route"] 中可见

RewriteRule ^(.*)?(.*)$ index.php?/$1&$2 [L]
<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 代码非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM