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?

4 Answers 4

62

You need the "QueryString Append" option:

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

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

6
  • 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
    Commented Jul 11, 2011 at 21:02
  • I'm not sure on that one. Could it be your missing variable name? Commented Jul 11, 2011 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
    Commented Mar 20, 2012 at 16:52
  • never saw that you refered to me in your answer, thank you :)
    – DonSeba
    Commented Jul 3, 2012 at 12:08
  • 1
    Note the fact that there cannot be a space between the QSA,L. [QSA,L]
    – johnsnails
    Commented Oct 14, 2013 at 4:47
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"]

1
RewriteRule ^(.*)?(.*)$ index.php?/$1&$2 [L]
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.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.