开发者

.htaccess: GET variables are lost in rewrite

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

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 开发者_如何学Cof the getvar-variable from my URL.

Here's my .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?


You need the "QueryString Append" option:

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

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


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"]


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

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

Can be quite usefull to hide your api code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜