Ivan Venediktov
Ivan Venediktov

Reputation: 158

.htaccess and $_GET confusion

I have a rather strange situation here. I have a three line .htaccess which is:

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

And a simple algorithm:

if (empty($_GET["step"])) 
    { don't do anything } 
else 
    {  if ($_GET["step"] == 'step2')
          { do something } ..

Now if I use signup.php?step=step2 I get an expected result, but if I use signup/step2/, var_dump( $_GET ) returns 0.

I tried all similar threads I found here, but can't find anything working. I might just be walking circles around something I can't notice.

Upvotes: 2

Views: 58

Answers (3)

Ivan Venediktov
Ivan Venediktov

Reputation: 158

The answer was a combination of Jon's and Justin's answers.

Options -MultiViews

RewriteEngine On
RewriteBase /
RewriteRule ^signup/([^/]+)/?$ signup.php?step=$1 [L,NC]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([A-Za-z0-9-]+)/?$ $1.php [L]

Upvotes: 0

Jon Lin
Jon Lin

Reputation: 143906

If you're using apache 2.0 or higher, you must strip off leading /'s from the pattern, because the rewrite engine strips them off before applying rules in a per directory context, and rules in htaccess files are all per directory (i.e. the directory the htaccess files are in).

Additionally, if you intend to append a .php extension to the end, you must check if the file actually exists, otherwise any 404 would lead to a 500 server error. You should also do that last:

RewriteEngine On

RewriteRule ^signup/([A-Za-z0-9-]+)/?$ /signup.php?step=$1 [L,QSA]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([A-Za-z0-9-]+)/?$ $1.php [L]

Upvotes: 2

Justin Iurman
Justin Iurman

Reputation: 19016

You have some errors in your code.

Replace it by this one

Options -MultiViews

RewriteEngine On
RewriteBase /

RewriteRule ^([^/]+)/?$ $1.php [L,NC]
RewriteRule ^signup/([^/]+)/?$ signup.php?step=$1 [L,NC]

Upvotes: 1

Related Questions