【问题标题】:What should we write for tenth segment in .Htaccess file?我们应该为 .Htaccess 文件中的第十段写什么?
【发布时间】:2015-09-28 21:50:59
【问题描述】:
RewriteRule ^products/(\d+)/(.*)/(.*)/(.*)/([^_]+)-_([^.]+)_([^.]+)_([^.]+)_([^.]+)_([^.]+).html$  /$5-$6-$7-$8-$9-$10/p$1 [L,NC,R=301]

上面的重定向规则将我从这个 URL 重定向

http://www.example.com/products/44/category/sub_category/sub_to_sub_category/product_name_with_multiple_under_scores.html

http://www.example.com/product-name-with-multiple-under-440/p44

在上述重定向规则中,产品名称(分数)的最后一个字符被读取为段 $1,即 44 并附加“0”。

我的问题是我们应该为第十段写什么?

【问题讨论】:

    标签: php .htaccess redirect


    【解决方案1】:

    根据 mod_rewrite 文档,您不能在 RewriteRule 中访问超过 9 个分组的部分:

    http://httpd.apache.org/docs/current/mod/mod_rewrite.html

    因此,为了实现您所追求的目标,您可以将一些现有组组合在一起,使总数低于 10。在您的示例中,您没有使用匹配 $2、$3 和 $4,因此您可以分组那些没有问题的人:

    RewriteRule ^products/(\d+)/((.*)/(.*)/(.*))/([^_]+)-_([^.]+)_([^.]+)_([^.]+)_([^.]+)_([^.]+).html$  /$3-$4-$5-$6-$7-$8/p$1 [L,NC,R=301]
    

    【讨论】:

    • 我对公共段进行了分组,但“$3”仍然读取第三段。
    • 或者试试这个:RewriteRule ^products/(\d+)/(.*)/([^_]+)-_([^.]+)_([^.]+)_([^.]+)_([^.]+)_([^.]+).html$ /$3-$4-$5-$6-$7-$8/p$1 [L,NC,R=301]
    猜你喜欢
    • 2015-09-09
    • 2011-04-19
    • 2021-03-08
    • 2011-04-22
    • 2011-05-16
    • 2019-10-07
    • 1970-01-01
    • 2020-09-06
    • 2013-04-03
    相关资源
    最近更新 更多