htaccess multiple rewrite rules

admin

Administrator
Staff member
I migrate my wordpress blog located at blog.example.com to a static page located at example.com/articles.

The path to the blog articles is always the same (/year/month/day/title.html). Therefore I created this rewrite rule for blog.example.com:

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

This works great and all old article urls are redirected correctly. Now I also want to redirect some rss-feed urls so that I don't have to change them on all the planets I'm subscribed to. So I extended my .htaccess file with the following rules:

Code:
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)

But now the last rule matches everything, so "blog.example.com/category/english/feed/" is not redirected to "<a href="https://www.example.com/categories/english/index.xml" rel="nofollow noreferrer">https://www.example.com/categories/english/index.xml</a>" but to "<a href="https://www.example.com/articles/category/english/feed/" rel="nofollow noreferrer">https://www.example.com/articles/category/english/feed/</a>"

If I remove the last rule the redirect of the three rss-feeds works as expected. How can I make sure that the last rule doesn't match all URLs? Is there a way to tell htaccess to try the rules one by one and stop after the first match? Or how could I change the last rule so that it doesn't match the rss-feed urls?