22

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 of 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?

Industrial
  • 41,400
  • 69
  • 194
  • 289

4 Answers4

62

You need the "QueryString Append" option:

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

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

Kevin Stricker
  • 17,178
  • 5
  • 45
  • 71
  • Thanks! This seems to be working well. A quickie: It appears that the ending slash is required for `$_GET` params to be caught. Would it be possible to have the cake and eat it, ie. be able to do both `welcome/test?avar=1` & `welcome/test/?avar=1`? Thanks! – Industrial Jul 11 '11 at 21:02
  • I'm not sure on that one. Could it be your missing variable name? – Kevin Stricker Jul 11 '11 at 21:05
  • Tested and works fine without the enging slash here. Used it for JQuery UI Autocomplete, because that always appends the `term` parameter. `RewriteRule ^service/autocomplete/(.+)$ autocomplete.php?type=$2 [QSA,L]`. – w5l Mar 20 '12 at 16:52
  • never saw that you refered to me in your answer, thank you :) – DonSeba Jul 03 '12 at 12:08
  • 1
    Note the fact that there cannot be a space between the QSA,L. [QSA,L] – johnsnails Oct 14 '13 at 04:47
  • wish i knew this before using php to parse the url and reset the get variables manually! – Dan Hastings Jul 15 '17 at 09:22
10

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

DonSeba
  • 2,098
  • 2
  • 16
  • 25
1
RewriteRule ^(.*)?(.*)$ index.php?/$1&$2 [L]
Naftali
  • 144,921
  • 39
  • 244
  • 303
0
<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.

Colin
  • 101
  • 8