【问题标题】:Apache htaccess re-write URL rules?Apache htaccess 重写 URL 规则?
【发布时间】:2011-06-20 23:03:21
【问题描述】:

我想在 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

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

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • RewriteRule 指令只是测试 URL 路径。如果在 .htaccess 文件中使用,每个目录的上下文前缀会在测试前被剥离,并在应用规则后附加。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-21
      • 1970-01-01
      • 2011-12-06
      • 2016-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多