Redirect vs RedirectMatch

user6835554 picture user6835554 · Sep 15, 2016 · Viewed 17.5k times · Source

I am new to this community though have heard a lot about it. So, I am here today to get solution to one of my queries regarding redirect 301 and redirectmatch 301.

I have tried to redirect a 404 page using Redirect 301 something like that

Redirect 301 /part1-url http://domain.com/part1-url/part2-url/part3-url.html

This rule worked as a nightmare and my destination URL started misbehaving afterwards and a repetitive string of /part2-url/part3-url.html get appended to my detination URL which becomes something like -

http:// domain.com/part1-url/part2-url/part3-url.html/part2-url/part3-url.html/part2-url/part3-url.html/part2-url/part3-url.html/part2-url/part3-url.html/part2-url&id=part3-url.html&var=part2-url&var2=part3-url

I then used RedirectMatch as follows:

RedirectMatch 301 ^/part1-url$ http:// domain.com/part1-url/part2-url/part3-url.html

and it started working fine.

I am not able to understand why this happened and how the 2nd one worked.

I would really appreciate your help.

Answer

Quentin picture Quentin · Sep 15, 2016

Redirect is supposed to redirect all URLs starting with the string. Since the URL you redirect to started with that string, naturally you instantly redirected again.

RedirectMatch redirects URLs that match a regular expression. You used $ to explicitly match the end of the URL as part of that. That means that "starting with" is not enough.