【问题标题】:htaccess multiple rewrite ruleshtaccess 多个重写规则
【发布时间】:2017-11-21 17:12:24
【问题描述】:

我将位于 blog.example.com 的 wordpress 博客迁移到位于 example.com/articles 的静态页面。

博客文章的路径始终相同(/年/月/日/title.html)。因此我为 blog.example.com 创建了这个重写规则:

RewriteCond %{HTTP_HOST} ^blog\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/articles/$1 [R=301,L]

这很好用,所有旧文章的网址都被正确重定向。现在我还想重定向一些 rss-feed url,这样我就不必在我订阅的所有星球上更改它们。所以我用以下规则扩展了我的 .htaccess 文件:

RedirectMatch 301 ^/category/english/feed https://www\.example\.com/categories/english/index\.xml
RedirectMatch 301 ^/category/deutsch/feed https://www\.example\.com/categories/deutsch/index\.xml
RedirectMatch 301 ^/tag/dev/feed https://www\.example\.com/tags/dev/index\.xml

RewriteCond %{HTTP_HOST} ^blog\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/articles/$1 [R=301,L]
.htaccess lines 1-8/8 (END)

但现在最后一条规则匹配所有内容,因此“blog.example.com/category/english/feed/”不会重定向到“https://www.example.com/categories/english/index.xml”而是“https://www.example.com/articles/category/english/feed/”

如果我删除最后一条规则,三个 rss-feed 的重定向将按预期工作。如何确保最后一条规则不匹配所有 URL?有没有办法告诉 htaccess 逐个尝试规则并在第一场比赛后停止?或者我如何更改最后一条规则,使其与 rss-feed url 不匹配?

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    将最后的重写改成这样:

    RewriteCond %{HTTP_HOST} ^blog\.example\.com$ [NC]
    RewriteCond %{REQUEST_URI} !feed/?$
    RewriteRule ^(.*)$ https://www.example.com/articles/$1 [R=301,L]
    

    【讨论】:

    • 感谢这几乎是完美的。唯一剩下的问题,这个重定向现在“blog.example.com/category/english/feed”正确,但不是“blog.example.com/category/english/feed/”。带有斜杠,它回退到 url ""example.com/articles/category/english/feed"
    • 它现在可以工作,但我不得不删除你的第二条规则。否则它将捕获 rss-feed 并将“blog.example.com/category/english/feed”重写为“example.com/category/english/feed”而不是“example.com/categories/english/index.xml ”。如果您调整答案,我将接受它作为解决方案。非常感谢您的帮助!
    猜你喜欢
    • 2015-02-15
    • 2013-06-29
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 2011-02-04
    • 2021-02-09
    • 2016-07-07
    相关资源
    最近更新 更多