(+84) 463.28.7979

Redirect Domain to Subdomain Using 301 Redirection


Is there any difference between http://wpbag.com and http://www.wpbag.com? As a normal user, I would say, no! But for a search engine, these are two websites hosted on different domains.

How does it impact websites?

If the same website is accessible via domains with and without the www part, search engines will treat it as a case of content duplication. They hate duplicate content as it clutters their search page with not-so-useful results. It makes sense to remove duplicate results for decluttering the results page. In this decluttering process, search engines will use their discretion to list either of the two URLs on its results page.

What’s the problem?

Even if a search engine uses its discretion to select between the URLs with and without www part, my webpage still appears in search.
Yes, it does, but the entire content of your website gets divided into two separate entities. This is like bifurcating a powerful nation into two separate countries. Won’t their clout get diminished? That’s exactly what happens when your website is accessible via both the domains. Your website’s search engine rank is affected.

How to solve this problem?

Stick to just one of the domains–either with or without www. Choice is entirely yours. Now the question: How to redirect a domain to a subdomain or vice versa?

Set a 301 URL redirection for the other domain. 301 redirection will tell Web browsers and search engines that the website has been moved permanently to the other domain and will redirect them to it.

If your website is hosted on Apache (Linux hosting) server, you can set a 301 (moved permanently) URL redirection placing an .htaccess file in the home directory (the top directory where your website points to). Note that an .htaccess file has a dot at the beginning and does not have any extension such as .txt or .htm.

How to create .htaccess file?

You can create an .htaccess file using text editors such as Notepad and Notepad++ in Windows. Open Notepad, and create a file by saving it as .htaccess (note the dot at the beginning of the name) without any extention. Don’t save it as .htaccess.txt.

Now paste the the following code and customize it for your website. Replace mydomain with your domain name and .com with the top-level domain of your website. In this sample code,http://mydomain.com is being redirected to http://www.mydomain.com.

1 RewriteEngine on
2 RewriteCond %{HTTP_HOST} ^mydomain.com$ [NC]
3 RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
4
5 #Deny access to htaccess
6 Order Allow,Deny
7 Deny from all

If I want to set 301 redirection from www subdomain of mywebsite.in to the main domain without www (http://mywebsite.in), the .htaccess code will have to be customized as give below:

1 RewriteEngine on
2 RewriteCond %{HTTP_HOST} ^www.mywebsite.in$ [NC]
3 RewriteRule ^(.*)$ http://mywebsite.in/$1 [R=301,L]
4
5 #Deny access to htaccess
6 Order Allow,Deny
7 Deny from all

The above method require mod_rewrite module to be active on the Apache server. Some servers may require you to add the following line to the .htaccess file:

1 Options +FollowSymLinks

Upload the file to your website’s root directory using Filezilla or other FTP clients. Now try accessing your website with and without www in the URL. If you type the non-preffered URL, it will be automatically redirected to the preferred one.

301 redirection using ASP

If your website is hosted on a Windows server, the .htaccess method will not work for you. On Microsoft’s Internet Information Services, the server-side script language is ASP or Active Server Pages. The redirection code must be located in a script that is executed on every webpage on the server before the page begins to load. Following is the ASP redirect script that needs to be executed before loading a page:

8 %>
7 End if
1 <%
2 If InStr(Request.ServerVariables("SERVER_NAME"),"www") = 0 Then
3 Response.Status="301 Moved Permanently"
4 Response.AddHeader "Location","http://www."
5 & Request.ServerVariables("HTTP_HOST")
6 & Request.ServerVariables("SCRIPT_NAME")

For adding the redirection code, create a file called 301moved.inc or anything.inc (using text editors) and paste the above code in it. Upload this file to the server and include it in the very first line of all ASP pages. (You might need to fix the path to the redirect file in different ASP files. Please note that the instructions for ASP redirection are based on my research. If you know exactly how to do it or a better way, please let me know.)

1 <!--#include file ="301moved.inc"-->

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>