Andrei Oniga
Andrei Oniga

Reputation: 8559

URL rewrite - issue reading a $_GET variable

I've written an URL rewrite rule to redirect URLs such as

http://picselbocs.com/projects/helsinki-map-application/{something} to 
http://picselbocs.com/projects/helsinki-map-application/index3.php?pcode={something}.

In the head of the index3.php file I'm checking if the $_GET['pcode'] variable is set, and my aim is, if the variable is NOT set (i.e. when trying to access http://picselbocs.com/projects/helsinki-map-application/), to redirect to the following address: http://picselbocs.com/projects/helsinki-map-application/D02081460

Unfortunately, the redirect does not occur. Meaning if the {something} value is empty, I'm not redirected to http://picselbocs.com/projects/helsinki-map-application/D02081460

The code within the php file is:

if (!isset($_GET['pcode'])) {
    header('Location: ./D02081460');    
    exit();
}

And the .htaccess file reads:

RewriteEngine On
RewriteRule    ^([A-Za-z0-9]+)?$    index3.php?pcode=$1    [NC,L]

Can anyone tell me what it is I'm doing wrong?

Thanks in advance!

Upvotes: 1

Views: 214

Answers (1)

ise
ise

Reputation: 228

At the moment, pcode will ALWAYS be set, it just MAY be empty because the rule will always pass and so you will get index3.php?pcode={something}, but {something} could equal to ''.

So either:

RewriteEngine On
RewriteRule    ^([A-Za-z0-9]+)/?$  index3.php?pcode=$1    [NC,L]
RewriteRule    ^.*                 index3.php             [NC,L]

Add a failover and make the group greedy so that it fails if nothing is passed. Or change your check to:

if ($_GET['pcode'] == '') {
    header('Location: ./D02081460');    
    exit;
}

Hope this helps,

ise

Upvotes: 4

Related Questions