mikelbring
mikelbring

Reputation: 1492

Modrewrite and PHP issues

I am trying to write this rewrite code and it just is not working (apache 2.2):

RewriteEngine on
RewriteBase /

RewriteRule ^account/activate\?token=(.*) index.php?route=account/activate&token=$1

I tried many different variations. After trying for about and hour and searching google I am stumped. This is where you come in!

Upvotes: 0

Views: 181

Answers (4)

Gumbo
Gumbo

Reputation: 655169

The pattern of the RewriteRule directive is only tested agains the URI path. The query string can only be tested with the RewriteCond directive:

RewriteCond %{QUERY_STRING} ^token=(.*)
RewriteRule ^account/activate$ index.php?route=account/activate&token=%1

But there’s a simpler solution: Just set the QSA flag to get the query string automatically appended to the new query string:

RewriteRule ^account/activate$ index.php?route=account/activate [QSA]

Note: You just need to set the QSA flag if there is a query defined in the substitution. If not, the original query already will automatically appended.

Upvotes: 2

zidar
zidar

Reputation: 764

Try this

RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^account/activate$ index.php?route=account/activate&%1

Basically this maps the entire query string to %1 in the RewriteRule and you can then access your token from your php-script using good old regular $_GET['token']

Upvotes: 3

jeje
jeje

Reputation: 3211

You cannot match the parameters part of your URL with mod_rewrite.

The only way to test for parameter in your URL is to use a RewriteCond directive and testing the env variable QUERY_STRING

Upvotes: 0

MrHus
MrHus

Reputation: 33378

I've used this:

RewriteEngine On

RewriteRule ^account/activate/?$ index.php?route=account/activate&%{QUERY_STRING}
RewriteRule ^account/activate/?/$ index.php?route=account/activate&%{QUERY_STRING}

And it works with this:

<?php 

echo "route is: " . $_GET['route'];
echo "</br>";
echo "token is " . $_GET['token'];

My directory where my code resides in is called test. These are my urls:

http://localhost:80/test/account/activate/?token=test
http://localhost:80/test/account/activate/?token=asd

Upvotes: 0

Related Questions