What’s causing this 301 redirect loop

301-redirectapache-2.2

I have several Redirect directives in my Ubuntu /etc/apache2/sites-available file:

    Redirect 301 /rss.xml http://example.com/feed/index.xml
    Redirect 301 /atom.xml http://example.com/feed/atom/index.xml
    Redirect 301 /feed/ http://example.com/feed/index.xml
    Redirect 301 /feed http://example.com/feed/index.xml

They all work correctly, so far as I can tell.

However, when I visit http://example.com/feed/index.xml with web-sniffer.net, it returns a 301 pointing at http://example.com/feed/index.xmlindex.xml, which then gets redirected to http://example.com/feed/index.xmlindex.xmlindex.xml, and so forth.

It seems that one of the patterns is matching when I don't expect it to. If I omit the last two redirects, the looping is stopped, but then I'm not redirecting /feed and /feed/ when I should.

Any ideas of how make this work correctly?

(I think the problem is related to this question: Infinite redirect loop?)

Best Answer

Redirect is simple prefix matching and "[a]dditional path information beyond the matched URL-Path will be appended to the target URL."

Thus /feed/index.xml matches your third rule /feed/ with additional path information index.xml so it gets redirected to /feed/index.xml with index.xml appended to it.

The solution is to use RedirectMatch and use anchors:

RedirectMatch 301 ^/rss.xml$ http://example.com/feed/index.xml
RedirectMatch 301 ^/atom.xml$ http://example.com/feed/atom/index.xml
RedirectMatch 301 ^/feed/$ http://example.com/feed/index.xml
RedirectMatch 301 ^/feed$ http://example.com/feed/index.xml