Home > Blockchain >  How to catch GET variables in a htaccess Rewrite Rule
How to catch GET variables in a htaccess Rewrite Rule

Time:11-05

i'm sorry i can't get out of this alone... after a successful payment users are redirected to this URL on my site:

https://www.example.com/orders/?token=AAA&status=OK

i would like to grab token and status and assign them to GET variables but i'm truly unable to do so, this is my current RewriteRule:

RewriteRule ^orders/?token=([^/] )&status=([^/] )$ /orders.php?lang=it&token=$1&result=$2 [L]

in orders.php i have this line:

echo "token: ".$_GET['token'];

but of course in console i always get

PHP Notice:  Undefined index: token in /orders.php on line 3

Can someone help me out?

CodePudding user response:

You can't match the query string using the RewriteRule pattern, you need to use a RewriteCond directive and check against the QUERY_STRING server variable. However, in this instance, you MUST also ensure that MultiViews is disabled (otherwise orders.php will be called without any URL parameters).

Try the following instead:

# MultiViews MUST be disabled
Options -MultiViews

# Rewrite URL with query string
RewriteCond %{QUERY_STRING} ^token=([^&] )&status=([^/] )$
RewriteRule ^orders/$ orders.php?lang=it&token=%1&result=%2 [L]

Note the use of the %1 and %2 backreferences (as opposed to $1 and $2) that get the values from the captured groups in the preceding condition, as opposed to the RewriteRule pattern.

  • Related