views:

101

answers:

4

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!

A: 

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

MrHus
I pasted what you had, and it did not work.
mikelbring
Ok did you try replacing you're entire .htaccess with my snippit. If that doesn't work I think its a configuration error. Maybe you could post the whole htaccess.
MrHus
It matches yours perfectly. The page comes up and the router var works but the token is empty.
mikelbring
I think we are on the wrong page, I want the url to display ?token= , such as /account/activate/?token=adasdasd
mikelbring
A: 

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

jeje
+2  A: 

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']

weazl
That worked thank you!
mikelbring
+1  A: 

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.

Gumbo