What is redirect ?
Generally it means direct something to a new or different place.What is URL redirect ?
URL redirection is also called URL forwarding and its a world wide web technique to increase the availability of a web page for more than one URL location. When browser send request to server for opening any redirected URL, the server forward the request to another URL.When do we need URL redirection ?
Migrating a website hosting never effect off-page SEO(Google ranking) but migrating a domain name URL always effect SEO and ranking factors. Changing website URL to new one always destruct search engine rankings.When we need to change our business name, off course we purchase a new domain name and migrate our existing domain name URL to new domain name. So we need to bring the user back to new URL and its called 301 permanent redirect.
I suggest URL change should never be done until and unless it’s absolutely necessary. URL change destruct ranking completely because your existing ranked URLs return 404 NOT FOUND error to google and as expected google kicks us out.
Even if we use
.htaccess
redirection process, it little effects our ranking but although ranking is reversed back after certain time but I personally suggest we should never follow URL change procedure unless its very urgent requirement.Forwarding a domain or sub domain is almost equivalent to redirecting a domain or sub domain. we can use these words interchangeably. Forwarding our domain name, sub domain or any URL path to another URL or to any
directories/sub-directories
is different process but use same concept. In this article , assuming that readers already understand well about domain name, sub domains & URL, we are going to cover complete guidance on redirecting/forwarding a URL.Redirection on server or client side ?
.htaccess
redirection is off-course server sided but client side direction is also possible via Javascript and redirect meta tag. Server redirection is fast and independent on .htaccess
. JavaScript or redirect meta tag needs browser to load DOM structure and then process the redirection.Sometime we need to show a web page URL for few seconds and then forward it to another URL. For this job, JavaScript redirection is the best choice.
Example of JS client side redirect.
<script>
// similar behavior as an HTTP redirect
window.location.replace("https://infoconic.com");
// similar behavior as clicking on a link
window.location.href = "https://infoconic.com";
</script>
Example of Meta tag redirect which is another client side redirection & needs to be placed in head section of page.
<!DOCTYPE html>
<html>
<head>
<title>HTML Meta Tag</title>
<meta http-equiv = "refresh" content = "2000; url = https://infoconic.com" />
</head>
<body>
<p>This is demo meta redirection to another URL.</p>
</body>
</html>
Well, many developers introduce domain forwarding as server side request but think logically that domain URL can be requested from server to server and browser to server as well. The server forwards the browser request to another URL and on client side redirection browser directly request the server. Server side example like If you forward
example.org
to example.com
then the browser send request to DNS of example.org
and server will forward this request to another DNS.In this article, I will provide maximum possible codes of redirecting/forwarding the URL using server side
.HTACCESS
.Actually Using Redirect/forwarding in an .htaccess file enables you to redirect users from an old url to a new url instantly. This redirect will be very fast & we won’t even notice the old URL path in address bar, because it happens on server side.
Let’s proceed with
HTACCESS
codes now. Below is the maximum possible forwarding/redirecting code for a domain or sub domain.Using htaccess (code to be used in .htaccess
file located on server)
1) Redirect a Domain name to another Domain name (we can say redirect a Domain name URL to another Domain URL)Below code will redirect all pages to new domain. For example
cerinsch.com redirects to https://infoconic.com
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^.*cerinsch\.com$ [NC]
RewriteRule ^(.*)$ https://infoconic.com/ [R=301,L]
2) Redirect a domain name to another domain name but changes only the domain name path , rest URL remains the same. For example
www.example.net/somepage.html?var=foo redirects to www.newdomain.com/somepage.html?var=foo
RewriteEngine on
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
3) Redirect a domain name to sub-domain name but changes only the domain name to sub-domain name , rest URL remains the same. For example
www.example.net/somepage.html?var=foo redirects to new.newdomain.com/somepage.html?var=foo
RewriteEngine On
RewriteRule (.*) http://new.domain.com/$1 [R=301,L]
4) Redirect a sub-domain name to a folder on the root domain that uses the same name. For example
http://abc.domain-name.com redirects to http://www.domain-name.com/abc
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.yourdomain\.com
RewriteRule ^(.*)$ http://www\.yourdomain\.com/subdomain/$1 [L]
5) Redirect a sub-domain name to a main domain name but here also path of URL will remain same. For example
http://blog.infoconic.com/demo.html/ redirects to http://www.infoconic.com/demo.html
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.example\.com [NC]
RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]
6) Redirect any non-www to www AND domain name to folder in root (dual work)
For example
https://infoconic.com redirects to https://www.infoconic.com/any_folder
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/webnius/
RewriteRule ^(.*)$ /webnius/$1 [L]
7) Redirect any domain name to folder in root.(without non-www to www) For example
http://www.infoconic.com redirects to http://www.infoconic.com/any_folder
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?YourDomain.com$
RewriteRule ^(/)?$ any_folder [L]
8) Redirect any sub-domain to folder in root.(when sub-domain already pointing to root).
For example
https://demo.infoconic.com redirects to https://demo.infoconic.com/any_folder
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} subdomain\.domain\.com
RewriteCond $1 !^folder-name
RewriteRule (.*) /folder-name/$1 [L]
9) Redirect specific URL to new URL(on same server)
For example
/path/index.html redirects to /path/home.html
Redirect /path/to/old/file/old.html /path/to/new/file/new.html
10) Redirect specific URL to new URL(on external server and 301 redirect)
For example
/path/index.html redirects to http://www.example.com/new/file/new.html
Redirect 301 /path/to/old/file/old.html http://www.example.com/new/file/new.html
11) Redirect from an index.html file to a different directory
For example
/index.html redirects to /new/
Redirect /index.html /new/
12) Redirect HTTP to HTTPS using htaccess (Use when you purchase and install SSL on server)
For example
infoconic.com redirects to https://infoconic.com
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]