Problem in regular expression in .htaccess

2

I have the following rules in my file .htaccess :

RewriteRule ^first-login/([a-zA-Z0-9]+)$ ./first-login.php?userKey=$1&step=1
RewriteRule ^first-login/([a-zA-Z0-9]+)?([0-9]+)$ ./first-login.php?userKey=$1&step=2

The first one works, I use the URL http://localhost/first-login/Xmi5drXyH9ngm4 and it fills the $_GET["userKey"] e $_GET["step"] variables with the correct values.

This page is a wizard with a few steps to the first user account configuration, at the end of each step, I redirect the user to the same page just changing the value of step , so I submit the content targeted. URL used: http://localhost/first-login/Xmi5drXyH9ngm4?2 .

The problem: even with ?2 at the end, I'm being redirected according to the first rule, so my variable $_GET["step"] is always getting the value 1. What wrong in these regular expressions? For now I'm doing the process of hiding / displaying content with jQuery.

    
asked by anonymous 21.04.2015 / 00:10

2 answers

1

As far as I know the mode RewriteRule it converts GETs from ?= to /

In my tests, the reason for not working is that the expression:

RewriteRule ^first-login/([a-zA-Z0-9]+)?([0-9]+)$ ./first-login.php?userKey=$1&step=2

is never captured because of ? that in url is / plus if you want to catch ? literal you should use \?

try changing the rule to:

RewriteRule    ^first-login/([a-zA-Z0-9]+)/([0-9]+)$ ./first-login.php?userKey=$1&step=2
    
22.04.2015 / 13:35
1

According to RegExr :

  

     

? Corresponds to 0 or 1 occurrences of the preceding element, making it optional.

on the other hand:

  

? Matches a "?" character (char code 63).

     

\? Matches a "?" character (code 63).

In order for your code to work as intended, escape the '?' in the second rule preceding it with '\'.

In addition, in the second rule use \? to represent the step digit in the URL.

The new code looks like this:

RewriteRule ^first-login\/([a-zA-Z0-9]+)$ ./first-login.php?userKey=$1&step=1
RewriteRule ^first-login\/([a-zA-Z0-9]+)\?([0-9]+)$ ./first-login.php?userKey=$1&step=$2 
    
02.06.2015 / 20:13