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

Disclosure:We receive compensation from companies whose products and services we feature. All links, coupons & recommendations on this website should be treated as paid advertisements.

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.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

#Deny access to htaccess
Order Allow,Deny
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:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.in$ [NC]
RewriteRule ^(.*)$ http://mywebsite.in/$1 [R=301,L]

#Deny access to htaccess
Order Allow,Deny
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:

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:

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

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.)

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