Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect vs RedirectMatch

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.

like image 916
user6835554 Avatar asked Sep 15 '16 14:09

user6835554


People also ask

What is the benefit of 301 redirect?

A 301 signals a permanent redirect from one URL to another, meaning all users that request an old URL will be automatically sent to a new URL. A 301 redirect passes all ranking power from the old URL to the new URL, and is most commonly used when a page has been permanently moved or removed from a website.

Why should I use redirects?

Redirects are important for both visitors and search engines when content has moved: Visitors: you want to offer visitors a great user experience on your website. When they're requesting content that was previously available on URL A , you don't want them to hit a 404-page. You want them to be redirected to URL B .

What is a 301 .htaccess redirect?

A 301 Permanent Redirect permanently redirects one URL to another. You set up a 301 redirect using . htaccess to send visitors to a new URL and tell search engines that a page has moved so that the new page can be properly indexed.


2 Answers

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.

like image 129
Quentin Avatar answered Sep 22 '22 03:09

Quentin


Taken from Apache.

Redirect:

The Redirect directive maps an old URL into a new one by asking the client to refetch the resource at the new location.

The old URL-path is a case-sensitive (%-decoded) path beginning with a slash. A relative path is not allowed.

The new URL may be either an absolute URL beginning with a scheme and hostname, or a URL-path beginning with a slash. In this latter case the scheme and hostname of the current server will be added.

Then any request beginning with URL-path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-path will be appended to the target URL.

Source: Apache Redirect

RedirectMatch

This directive is equivalent to Redirect, but makes use of regular expressions, instead of simple prefix matching. The supplied regular expression is matched against the URL-path, and if it matches, the server will substitute any parenthesized matches into the given string and use it as a filename.

Source: Apache RedirectMatch

like image 30
Slico Avatar answered Sep 24 '22 03:09

Slico