Using htaccess for redirection.
Some useful information about Redirection using .htaccess.
Apache web server provides several way for setting up redirects. The most simple one is using the “Redirect” directive.
REDIRECT DIRECTIVE
Redirects we use:
================
# permanent - (301) resource has moved permanently.
# temp - (302). resource has moved temporarily.
# seeother - (303) resource has been replaced.
================
* Redirecting a particular directory to a remote website.
================
redirect 301 /source_dir/ http://www.new-site.com/
================
The status "301" can be replaced according to the requirement.
* Redirect an entire site.
================
redirect 301 / http://www.new-site.com/
================
* Redirecting a file
================
redirect 301 /path_to_file http://www.new-site.com/new_file
================
REWRITE RULE:
The RewriteRule is more powerful than the Redirect rule, and thus should serve the redirection more effectively.
Tips:
In order to get an idea about RewriteRule, please check the rule given below.
RewriteRule ^(.*)$ http://www.new-domain.com/$1 [R=301]
The above rule will redirect the old website to the new website new-domain.com. It contains the following parts.
1) (.*) — Is a regular expression. The request string is everything that comes after the slash after the domain name. So if you request the URL “http://old_domain/test_pattern”, the request string that get tested against this pattern is “test_pattern”.
This particular pattern, “(.*)”, will match anything. It even successfully matches a empty string, which means it will successfully match when we request the URL “http://old_domain/”.
2) http://www.new-domain.com/$1 — the replacement pattern. When the regular expression in the first part matches, this pattern describes how to rewrite the URL. $1 is a reference to the text captured by the sub-pattern within the parentheses; if we had a pattern with more than one set of parentheses, we can use $2, $3, etc. So in the above example $1 will contain the value "test_pattern"
That means http://www.old_domain.com/$1 will be redirected exactly to http://www.new-domain.com/test_pattern
3) [R=301] - this is a flag that says that this redirection is permanent. That means the HTTP redirection goes out with a 301 code.
Examples:
* Redirect from Old domain to new domain.
================
RewriteEngine On
RewriteRule ^(.*)$ http://www.new-site.com/$1 [R=301,L]
================
* Redirect from http://www.domain.com to http://domain.com
================
RewriteEngine on
RewriteCond % ^www\.domain\.tld$ [NC]
RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]
================
RewriteCond tells that if the if HTTP_HOST is www.domain.tld, then apply the rewrite rule.
* Redirect a URL that starts with a given string.
================
RewriteEngine on
RewriteRule ^test http://www.new-site.com/ [R=301]
================
The above rule specifies that URLS like http://old_site.com/test_whatever will be forwarder to the URL http://www.new-site.com/
* To redirect a URL that exactly matches a given string.
================
RewriteEngine on
RewriteRule ^test$ http://www.new-site.com/[R=301]
================
The above rule will redirect http://old_domain.com/test/ to http://www.new-site.com/. This can be used for redirecting directories to remote URLs.
REDIRECTMATCH
RedirectMatch can be used to direct a different string to a correct directory.
================
RedirectMatch ^/director(y|ies) http://www.domain.com/exact_directory
================
The above rule will redirect the URLs http://domain.com/directory and http://domain.com/directories to the URL http://www.domain.com/exact_directory.
Thank you.