Jump to content
Sign in to follow this  
Richard_Grant

trailing slash is the DEVIL!

Recommended Posts

I got these 2 rewrites:

RewriteRule ^user/([0-9]+)/(.*)/(.*)/ ./board/user.php?ID=$1&FIRSTNAME=$2&LASTNAME=$3 [L,NC]
RewriteRule ^user/([0-9]+)/(.*)/(.*) ./board/user.php?ID=$1&FIRSTNAME=$2&LASTNAME=$3 [L,NC]

And i want to combine them but when i do this:

RewriteRule ^user/([0-9]+)/(.*)/(.*)(/|$) ./board/user.php?ID=$1&FIRSTNAME=$2&LASTNAME=$3 [L,NC]

It doesnt work..

 

when i type this in the url  https://www.mysite/board/users.php?ID=1&FIRSTNAME=Richard&LASTNAME=Grant/ the variables are retrieved like this: (has trailing slash)

ID="1"

FIRSTNAME="Richard/Grant"

LASTNAME=""

 

 

 

when i type this in the url  https://www.mysite/board/users.php?ID=1&FIRSTNAME=Richard&LASTNAME=Grant the variables are retrieved like this: (doesn't have trailing slash)

ID="1"

FIRSTNAME="Richard"

LASTNAME="Grant"

 

 

Share this post


Link to post
Share on other sites

RewriteRule ^user/([0-9]+)/(.*)/(.*)(/|$) ./board/user.php?ID=$1&FIRSTNAME=$2&LASTNAME=$3 [L,NC]
That last (.*) is going to match everything up to the end of the string, including the slash. When the regex engine has to choose between / and the end of the string, it'll choose the latter because that's already true and it doesn't want to backtrack to match the slash.

 

Be more restrictive than just .* for everything. Even if you want to allow all sorts of characters you know slashes won't be allowed, right? You should also enforce the end of the string, otherwise /1/Richard/Grant/other/stuff will be allowed.

RewriteRule ^user/([0-9]+)/([^/]+)/([^/]+)/?$ ./board/user.php?ID=$1&FIRSTNAME=$2&LASTNAME=$3 [L,NC]
Edited by requinix

Share this post


Link to post
Share on other sites
RewriteRule ^user/([0-9]+)/(.*)/(.*)(/|$) ./board/user.php?ID=$1&FIRSTNAME=$2&LASTNAME=$3 [L,NC]
That last (.*) is going to match everything up to the end of the string, including the slash. When the regex engine has to choose between / and the end of the string, it'll choose the latter because that's already true and it doesn't want to backtrack to match the slash.

 

Be more restrictive than just .* for everything. Even if you want to allow all sorts of characters you know slashes won't be allowed, right? You should also enforce the end of the string, otherwise /1/Richard/Grant/other/stuff will be allowed.

RewriteRule ^user/([0-9]+)/([^/]+)/([^/]+)/?$ ./board/user.php?ID=$1&FIRSTNAME=$2&LASTNAME=$3 [L,NC]

 

Works :) just like i needed!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.