Problem with .htaccess and RewriteRule
I have url's like games/xbox/2
2 being the page number I need the url rewritten. This is what I'm using:RewriteRule games/(.*开发者_StackOverflow中文版?)/$ games/consoles.php?console=$1
RewriteRule games/(.+?)/(.+?)/$ games/consoles.php?console=$1&page=$2
The first rule works fine but the second is returning consoles.php as $1 instead of xbox
RewriteRule games/([A-Za-z0-9]+)/?$ games/consoles.php?console=$1
RewriteRule games/([A-Za-z0-9]+)/([0-9]+)/?$ games/consoles.php?console=$1&page=$2
Using (.*?)
would match even the /
character so xbox/2 is treated as a whole
Try something like:
RewriteRule games/([^/]+)/([^/]+)/?$ games/consoles.php?console=$1&page=$2 [L]
RewriteRule games/([^/]+)/?$ games/consoles.php?console=$1 [L]
I first put your most specific rule first - that way you don't do a general match, then a later more specific match mangles that general rewrite.
I also specified the [L]
flag to signify that you want the engine to stop looking for more matches at this point. Re-ordering the rules is redundant in this case because of the [L]
flag, but it's a good practice to get into.
I also changed the expressions slightly. Rather than using ([A-Za-z0-9]+)
like the previous poster said, I changed it to ([^/]+)
because that will match everything but a slash, so you can have weird console or game names. If you want to make it more specific feel free to, but this way provides the most general use-case.
精彩评论