Here's the htaccess rule for my site's profile page:
RewriteRule ^users/([0-9]+)/?(.+)?/?(.+)? profile.php?id=$1&slug=$2&tab=$3&%{QUERY_STRING}
When you click on a tab on a user's profile, such as favorites:
/users/83028/nathan-johnson/favorites
The $_GET
variable with the name of slug is receiving nathan-johnson/favorites
instead of just nathan-johnson
and the tab
variable is blank, according to a var_dump
:
array(3) {
["id"]=> string(5) "83028"
["slug"]=> string(24) "nathan-johnson/favorites"
["tab"]=> string(0) "" }
It looks like the issue is with my htaccess regexp (above), but I don't see the issue with it.
Thanks in advance.
Because your forward slashes are optional (via /?
), the (.+)
grouping is greedy and will gobble them all up. You need to make them non-greedy by adding a ?: (.+?)
Also, instead of (.+)?
you should just use a *
which means none, one, or more.
Try:
RewriteRule ^users/([0-9]+)/?([^/]*)/?(.*?)?$ profile.php?id=$1&slug=$2&tab=$3&%{QUERY_STRING}
Also, instead of using %{QUERY_STRING}
, you can just use the QSA
flag:
RewriteRule ^users/([0-9]+)/?([^/]*)/?(.*?)?$ profile.php?id=$1&slug=$2&tab=$3 [L,QSA]