Apache htaccess重写URL规则?

时间:2009-07-06 10:04:03

标签: php apache .htaccess url-rewriting friendly-url

我想为htaccess中的后续网址创建URL重写规则。

http://example.com/vedios/category/fun/ -> http://example.com/vedios/category.php?cat=fun

http://example.com/vedios/category/fun/?show=popular -> http://example.com/vedios/category.php?cat=comedy&show=popular

http://example.com/vedios/category/fun/?show=popular&page=2 -> http://example.com/vedios/category.php?cat=comedy&show=popular&page=2

http://example.com/vedios/category/fun/comedy/ -> http://example.com/vedios/category.php?cat=comedy

http://example.com/vedios/category/fun/comedy/?show=popular -> http://example.com/vedios/category.php?cat=comedy&show=popular

http://example.com/vedios/category/fun/comedy/?show=popular&page=3 -> http://example.com/vedios/category.php?cat=comedy&show=popular&page=3

我为RewriteRule category/([^.]+)/?([^.]+)$ /vedio/category.php?cat=$1&$2尝试了http://example.com/vedio/category.php?cat=fun&show=popular&page=1,但它无效。

请告诉我上述要求的正确URL重写规则是什么?

2 个答案:

答案 0 :(得分:4)

查询不是在RewriteRule指令中检查的URL路径的一部分。你需要一个RewriteCond指令才能做到这一点。

但您只需设置QSA flag即可将初始查询字符串附加到新的查询字符串中:

RewriteRule category/([^.]+)/$ /vedio/category.php?cat=$1 [QSA]

答案 1 :(得分:0)

我的类别脚本的正则表达式:

/vedio/category/([^.]+)/\?([^.]+)$

这取决于mod_rewrite是在评估您的所有URL(服务器+端口+路径)还是仅仅是您脚本的路径。