【问题标题】:Redirecting URL with .htaccess使用 .htaccess 重定向 URL
【发布时间】:2013-12-05 20:04:03
【问题描述】:

我想使用 .htaccess 重定向 URL。我有以下代码:

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?action=$1&id=$2

如果我不在 URL 中使用点,这将正常工作。但是,如果我想访问例如这个 URL:

load/com.example.unique.id

然后我会收到错误 404。当然,我可以理解为什么会这样:我的表达式中没有包含点。但是,如果我包含它,我会收到错误 500。

我尝试在这样的表达式中包含点:

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-\.]+)$ index.php?action=$1&id=$2

知道为什么会这样吗?谢谢!

【问题讨论】:

    标签: php regex apache .htaccess redirect


    【解决方案1】:

    我认为问题在于 [a-zA-Z0-9_-.] 中的“-”,试试这个:

    RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_\-.]+)$ index.php?action=$1&id=$2
    

    【讨论】:

      【解决方案2】:

      您可以使用点,但连字符需要在字符类的最后或第一个位置以避免转义。正确的基于正则表达式的规则是这样的:

      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^([\w.-]+)/([\w.-]+)/?$ index.php?action=$1&id=$2 [L,QSA]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-16
        • 2011-01-27
        • 2011-03-23
        • 1970-01-01
        相关资源
        最近更新 更多