Regular expression problem in . htaccess

Asked

Viewed 468 times

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 he fills in the variables $_GET["userKey"] e $_GET["step"] with the right 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 by changing the value of the step, then I present the targeted content. URL used: http://localhost/first-login/Xmi5drXyH9ngm4?2.

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

2 answers

1

According to Regexr:

? Matches 0 or 1 of the preceding token, effectively making it optional.

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

on the other hand:

\? Matches a "?" Character (char code 63).

\? It matches a character "?" (code 63).

For your code to work as desired, escape the '?' in the second rule by preceding it with '\'.

Also, in the second rule use $2 to represent the step digit in the URL.

The new code would look 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 

1

From what I know the mode RewriteRule he converts the GETs of ?= for /

In my tests, the reason it’s 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 the url is / besides that if you want to capture the ? literal you must use \?

try to change the rule to :

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.