2017-06-19 197 views
0

我將位於blog.example.com的wordpress博客遷移到位於example.com/articles的靜態頁面。htaccess多個重寫規則

博客文章的路徑始終是相同的(/year/month/day/title.html)。因此,我創建了這個blog.example.com重寫規則:

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

這個偉大的工程,所有舊的文章網址是否正確重定向。現在我也想重定向一些rss-feed網址,這樣我就不必在我訂閱的所有星球上改變它們。所以,我伸出我的以下規則.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-feeds的重定向按預期工作。我如何確保最後一條規則不符合所有網址?有沒有辦法告訴htaccess逐一嘗試規則,並在第一場比賽後停止?或者我如何更改最後一條規則,使其與rss-feed url不匹配?

回答

1

改變最後重寫規則這樣:

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

由於這幾乎是完美的。剩下的唯一問題是,這個問題現在重定向爲「blog.example.com/category/english/feed」,但不是「blog.example.com/category/english/feed/」。隨着斜線,它回落到URL「」https://www.example.com/articles/category/english/feed/「 –

+0

它按預期工作嗎? –

+0

@Björn檢查我編輯的答案 –