views:

162

answers:

2

In my .htaccess, I have the following

RewriteEngine On

RewriteRule ^users/?$   users.php
RewriteRule ^users/([a-z]+)/?$   users.php?username=$1

Everything works as intended if I do

http://example.com/users/
http://example.com/users/joeschmoe/

and PHP will read "joeschmoe" as the value for

 $_GET['username']

However, if I do

http://example.com/users/joeschmoe/?foo

PHP will not pick up

 $_GET['foo']

Any idea why this is happening and how I can get it work? Thanks for your time!

+6  A: 

Add [QSA] option to your RewriteRule, which will enable apache to append query string to redirected url:

RewriteRule ^users/?$   users.php [QSA]
RewriteRule ^users/([a-z]+)/?$   users.php?username=$1 [QSA]
Ivan Nevostruev
thanks, worked perfectly
Axsuul
+3  A: 

You are probably looking for the "QSA" option to RewriteRule :

'qsappend|QSA' (query string append)
This flag forces the rewrite engine to append a query string part of the substitution string to the existing string, instead of replacing it. Use this when you want to add more data to the query string via a rewrite rule.

That page also states :

Modifying the Query String

By default, the query string is passed through unchanged.
You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string.
When you want to erase an existing query string, end the substitution string with just a question mark.
To combine new and old query strings, use the [QSA] flag.


In your case, something like this should do :

RewriteEngine On

RewriteRule ^users/?$   users.php
RewriteRule ^users/([a-z]+)/?$   users.php?username=$1 [QSA]
Pascal MARTIN
thanks, your solution worked
Axsuul