0

Trying to write a rewrite rule to capture two GET variables

http://stackoverflow.com/blogs/category/general/1

RewriteRule ^blogs/category/(.+)/?$ blogs.php?category=$1 [PT,L,QSA]
RewriteRule ^blogs/category/(.+)/([0-9]+)/?$ blogs.php?category=$1&page=$2 [PT,L,QSA]

However when I grab these from the headers it looks like this?

$_GET['category'] = "general/1";
$_GET['page'] = "";

As you can see I have two rules, one for just when they provide category and one for when they also provide page number. Might be wrong about that approach I'm not sure.

What am I doing wrong here? How can I separate these variables properly using the rewrite rules (I know I could hack it in php but that's ugly)

4

1 回答 1

1

我认为您只需要切换它们,以便首先处理更具体的:

RewriteRule ^blogs/category/(.+)/([0-9]+)/?$ blogs.php?category=$1&page=$2 [PT,L,QSA]
RewriteRule ^blogs/category/(.+)/?$ blogs.php?category=$1 [PT,L,QSA]

进一步解释一下:如果没有另外指定,所有正则表达式都是贪婪的。这意味着,正则表达式试图获得尽可能多的东西。(.+) 匹配“general/1”。

于 2013-04-14T17:39:52.990 回答