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

My htaccess redirects with regex aren't working

提问人:AngryMob 提问时间:10/6/2023 更新时间:10/6/2023 访问量:20

问:

我的htaccess中有以下重定向:

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

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

我尝试了各种版本的语法,什么都没有

正则表达式 .htaccess http-status-code-301

评论

0赞 arkascha 10/6/2023
apache 模块的文档,作为开源软件的典型,质量上乘,并附有很好的示例。当您手头有这样的任务时,非常值得一看。

答:

1赞 Patrick Janser 10/6/2023 #1

该指令仅将静态 URL 作为输入。Redirect

要解决它,请:

  • 替换为 RedirectMatchRedirect

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

    在这里,将收到 URL 的前导斜杠。RedirectMatch

  • 使用 RewriteRule

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

    在本例中,接收不带前导斜杠的路径。RewriteRule