简体   繁体   English

重写.htaccess 2 GET变量不拆分?

[英]Rewrite .htaccess 2 GET variables not splitting?

Trying to write a rewrite rule to capture two GET variables 尝试编写重写规则以捕获两个GET变量

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) 如何使用重写规则正确分离这些变量(我知道我可以用php修改它,但这很丑陋)

I think you just have to switch them so that the more specific one is handled first: 我认为您只需要切换它们,以便首先处理更具体的一个即可:

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

To explain that a little bit further: All regular expressions are greedy if not specified otherwise. 进一步说明:如果没有另外指定,所有正则表达式都是贪婪的。 Which means, that the regular expression tries to get as much as possible. 这意味着,正则表达式试图获得尽可能多的结果。 (.+) matches "general/1". (。+)匹配“ general / 1”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM