【问题标题】:Help me write a Regex RewriteRule for htaccess帮我为 htaccess 写一个 Regex RewriteRule
【发布时间】:2010-09-18 07:04:12
【问题描述】:

我需要帮助为 htaccess 中的 mod_rewrite 编写 RewriteRule 的简单正则表达式。所以,这就是我想要完成的:

books/2010-the-world-by-hopkins-139_PPS-1234567

应该去

index.php?pagename=mypage&PPS=1234567&description=2010-the-world-by-hopkins-139

因此,在pseducode中,正则表达式必须将books之后的部分拆分为_,我应该将其分成两部分:

  1. PPS(它始终是具有 1+ 个可变位数的数字)。这是_PPS-之后的部分
  2. 描述(它总是一个字符串)。这是在 _ 之前包含 ANYTHING 的部分。

我猜 RewriteRule 会是这样的:

RewriteRule books/(.*)_(.*) index.php?pagename=mypage&PPS=$2&description=$1

但我需要正确的正则表达式。请帮忙。

【问题讨论】:

    标签: regex .htaccess


    【解决方案1】:

    类似:但翻转 $1 和 $2 ;)

    ^books/(.*)_PPS-([0-9]{1,})$
    

    【讨论】:

    • 请注意,您将给定 url 的 VALIDATION 与 REWRITING url 同时混合,尤其是在 Regex 的第二组中。考虑将其分开并始终在 index.php 中再次验证查询字符串。
    猜你喜欢
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    相关资源
    最近更新 更多