TimNguyenBSM
TimNguyenBSM

Reputation: 827

.htaccess cannot catch GET parameter

.htaccess

RewriteRule ^apply/import?$ index.php?action=import [NC]
RewriteRule ^apply/import/([0-9a-zA-Z]+)/?$ index.php?action=import&code=$1 [NC]

I run the following url's and print_r($_GET)

"localhost/apply/import" //Prints Array ( [action] => import ) as expected

"localhost/apply/import?code=abc123" // Prints Array ( [action] => import ) only, and is missing code.

How do I catch get variable code?

Upvotes: 0

Views: 449

Answers (1)

Alireza Mirian
Alireza Mirian

Reputation: 6662

With "localhost/apply/import?code=abc123", the first rule is matched (you can check it by removing second rewrite code)
If you want to trigger second rewrite rule, you should try something like localhost/apply/import/abc123 and this will be the result:

Array ( [action] => import [code] => abc123)

UPDATE: I think this can solve your problem:
You can use [QSA] to get extra Query String parameters, in your first rewrite rule:

RewriteRule ^apply/import?$ index.php?action=import [QSA]

Then going to localhost/apply/import?code=abc123 will output this:
Array ( [action] => import [code] => abc123)

You can read more about RewiteRule Flags. Here is the explanation:

When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

Upvotes: 3

Related Questions