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
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
Link to comment
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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.