Jump to content

trailing slash is the DEVIL!


Richard_Grant

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"

 

 

Link to comment
https://forums.phpfreaks.com/topic/291055-trailing-slash-is-the-devil/
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]
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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

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.