【问题标题】:underscore and dash are not recognized in a variable变量中无法识别下划线和破折号
【发布时间】:2023-03-05 23:21:01
【问题描述】:

我的 .htaccess 中有以下规则

RewriteRule ^([\da-z]+)/([\da-z]+)/(\d+)/?$ index.php?office=$1&page=$2&news_id=$3 [L,QSA,NC]
RewriteRule ^([\da-z]+)/([\da-z]+)/?$ index.php?office=$1&page=$2 [L,QSA,NC]

当我有这样的网址时:www.test.com/media/continuous-training 它不会将持续训练发送到变量。如果我不使用破折号或下划线,一切都很好。

我认为问题出在规则中的正则表达式中,但不知道如何解决。

谢谢!

【问题讨论】:

    标签: php regex apache .htaccess seo


    【解决方案1】:

    您需要将-_ 添加到您的角色类中。使用这个:

    RewriteRule ^([\da-z_-]+)/([\da-z_-]+) ...
    

    【讨论】:

      【解决方案2】:

      你最好在你的字符类中使用[W-]来匹配[A-Za-z0-9_-]

      RewriteRule ^([\w-]+)/([\w-]+)/(\d+)/?$ index.php?office=$1&page=$2&news_id=$3 [L,QSA]
      RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?office=$1&page=$2 [L,QSA]
      

      【讨论】:

        猜你喜欢
        • 2015-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-19
        • 2010-09-12
        • 1970-01-01
        相关资源
        最近更新 更多