Retrieve parameter that without prarameter name

Viewed 60

I have a registration and activation code that I am updating a bit, after the user registers it sends them a email to activate.

Link emailed

/user/activate/$hashed_confirm_code

which generates

http://example.com/user/activate/0dbc0aef3d4d9d3e67ee4bf6c

I went to http://www.visiospark.com/mod-rewrite-rule-generator/ and did a mod rewrite changed the email link accordingly.

rewrite is on

RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]

RewriteCond %{QUERY_STRING} (^|&)hash_token=($|&)
RewriteRule ^user/activate$ /user/activate/?&%{QUERY_STRING}

if i click the link it produces the following in the browser

http://example.com/user/activate/0dbc0aef3d4d9d3e67ee4bf6c

however it just gives a 500 error, the basics of my activation code function is

function activate_user(){

    if (($_SERVER['REQUEST_METHOD'] === "GET") && isset($_SERVER['QUERY_STRING'])) {

        $confirm_code = $_SERVER['QUERY_STRING'];

       //do stuff


    }
}

now if i change the link in the browser

example.com/user/activate/0dbc0aef3d4d9d3e67ee4bf6c93c

to this

example.com/user/activate?0dbc0aef3d4d9d3e67ee4bf6c

it works, so if i remove the forward slash and add the question mark it works

1 Answers

The rewrite mode is not correct, you should get the matched variable and pass it tho the file (here I assumed it called activate.php) as a query string parameter:

RewriteRule ^user/activate/([^/]+)/?$ user/activate.php?code=$1 [L,NC]

PS: I used following flags:

L to cause mod_rewrite to stop processing the rule set.

NC to cause the RewriteRule to be matched in a case-insensitive manner.

You can also use QSA to make the query strings get combined.

Update:

RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]

These two rules will create a loop, why? Because first one says if you get a request that contains .php, go ahead and remove the .php file and second one indicates if you received a request that would be valid file name if we add .php, go ahead and add the extension to it. The important thing that you need to consider is whenever you change the url, apache starts from top and tries to mach with the new url. To prevent that you can use at top of the file:

#PREVENT SECOND EVALUATION
RewriteCond %{ENV:REDIRECT_STATUS} !^$
RewriteRule .* - [L,QSA]
Related