我的 htaccess 正则表达式重定向不起作用

问题描述 投票:0回答:1

我的 htaccess 中有以下重定向:

重定向 301“^/city/(.*)-north-carolina$”“https://example.com/US/NC/$1-north-carolina-hotels.php”

当我输入 example.com/city/asheville-north-carolina 时,我没有得到应有的重定向。我的语法有问题或者 RewriteCond 需要发生什么事情吗?

我尝试了各种版本的语法,没有任何结果

regex .htaccess http-status-code-301
1个回答
0
投票

Redirect
指令仅接受静态 URL 作为输入。

要解决此问题,可以:

  • Redirect
    替换为
    RedirectMatch
     :

    RedirectMatch 301 ^/city/(.*)-north-carolina$ https://example.com/US/NC/$1-north-carolina-hotels.php
    

    在这里,

    RedirectMatch
    将收到 URL 的前导斜杠。

或者

  • 使用

    RewriteRule
     :

    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^city/(.*)-north-carolina$ https://example.com/US/NC/$1-north-carolina-hotels.php [R=301,L]
    

    在这种情况下,

    RewriteRule
    接收不带前导斜杠的路径。

© www.soinside.com 2019 - 2024. All rights reserved.