I'm creating friendly links in my site, but I have a problem. I wrote this, but only the first line of code is working, and I'n not sure why:
RewriteRule ^user/([0-9a-zA-Z]+) user.php?user=$1 [NC,L]
RewriteRule ^user/([0-9a-zA-Z]+)/options user.php?user=$1&options [NC,L]
When I go to test.com/user/tom
everything is fine, but when I try test.com/user/tom/options
it just loda the same page.
I have php script that should load different pages, for different $_GET
, and its working if used with normal links.
The correct thing to do here is not to swap the lines around. Whilst that may fix your problem, it will also send user/tom/foo/bar
to the first rule. The reason it does this is because the pattern is not closed with $
. As such, your rules should read as follows:
RewriteRule ^user/([0-9a-zA-Z]+)$ user.php?user=$1 [NC,L]
RewriteRule ^user/([0-9a-zA-Z]+)/options$ user.php?user=$1&options [NC,L]
Tip: If you also have other pages for the user, you can simplify the second rule to include different segments by means of a capture:
RewriteRule ^user/([0-9a-zA-Z]+)/(options|other-page)$ user.php?user=$1&$2 [NC,L]
Also giving consideration to the fact that you are using the
NC
(no case) flag, you can drop theAZ
in your capture groups. You can also use\\d
for digits. Example[az\\d]+
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.