Apache htaccess重写URL规则?

时间:2021-07-16 10:43:15

I want to create URL re-write rules for following URL in htaccess.

我想为htaccess中的后续URL创建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

I tried RewriteRule category/([^.]+)/?([^.]+)$ /vedio/category.php?cat=$1&$2 for http://example.com/vedio/category.php?cat=fun&show=popular&page=1 but its not working.

我尝试了RewriteRule类别/([^.]+)/?([^.]+)$ /vedio/category.php?cat=$1&$2 for http://example.com/vedio/category.php?cat = fun&show = popular&page = 1但它不起作用。

Please tell me what could be the correct URL re-write rules for the above requirements?

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

2 个解决方案

#1


The query is not part of the URL path that’s checked within the RewriteRule directive. You would need a RewriteCond directive to do that.

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

But you just need to set the QSA flag to get the initial query string appended to the new one:

但是您只需要设置QSA标志以将初始查询字符串附加到新的标记字符串:

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

#2


My regexp for category script:

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

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

It depends if mod_rewrite is evaluating all your URL (server + port + path) or just the path to your script.

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

#1


The query is not part of the URL path that’s checked within the RewriteRule directive. You would need a RewriteCond directive to do that.

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

But you just need to set the QSA flag to get the initial query string appended to the new one:

但是您只需要设置QSA标志以将初始查询字符串附加到新的标记字符串:

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

#2


My regexp for category script:

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

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

It depends if mod_rewrite is evaluating all your URL (server + port + path) or just the path to your script.

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