【问题标题】:How to ignore apostrophe in url via .htaccess如何通过 .htaccess 忽略 url 中的撇号
【发布时间】:2014-09-25 14:43:21
【问题描述】:

我在我的 .htaccess 中使用以下规则

RewriteRule ^([\w-]*)/(.*)/t/(([\d]|[\-])+)$ pronew/ci/touch.php/touch/TouchSearchController?fl-brand=$2&category=$3 [L,QSA]

所以当我访问 http://local.xyz.com/personal-care-men's-toiletries/t/12345 网址时,它给了我。

Array
(
    [fl-brand] => ci/index.php/personal-care-men's-toiletries
    [category] => 12345
)

在 $_GET 但如果我从 url 中删除单引号,我不会在 $_GET 中得到 fl-brand 参数,这是正确的行为。

如何忽略 url 中的单引号,以便我只得到

Array
(
    [category] => 12345
)

【问题讨论】:

  • \w 是“字母数字字符”,其中不包括 '。您必须改用[\w\-']。请注意额外的转义,以防止 - 被视为范围运算符。
  • 您似乎有 4 个片段:[\w-]*.*t([\d]|[\-])+(可以简化为 [\d-]+)。但是您的网址只有 3 个:personal-care-men's-toiletriest12345
  • @MarcB 或者您可以在字符类的开头或结尾包含-[\w'-]

标签: php regex apache .htaccess mod-rewrite


【解决方案1】:

您可以在第一部分使用这个基于否定的正则表达式,在最后一部分使用\d+

RewriteRule ^([^/]+)/t/([0-9]+)/?$ pronew/ci/touch.php/touch/TouchSearchController?fl-brand=$1&category=$2 [L,QSA]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-07
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 2016-04-23
    • 1970-01-01
    相关资源
    最近更新 更多