How to redirect www to non www and vice-versa

In one of my earlier articles, I talked about URL redirection, covering why website owners choose to redirect their websites to a different domain, the difference between Permanent Redirect (301) and 302 Redirect, its implications on SEO and, in turn, your website. Once you’ve decided to redirect your website, it is equally important to decide, how you want to execute it.

Technically there are two URLs, www and non-www and, although these two URLs are different, if you have both of them redirecting to the same web page it may confuse users and affect your SEO and ranking. However, before delving into which URL to choose and how to redirect www to non-www and vice-versa, let us first see the difference.

WWW and Non-WWW: The Difference

WWW:

  1. ‘www’ stands for ‘world wide web’ and websites beginning their domain name with ‘www’ are the most common choice for any website.
  2. The prefix acts as a hostname, easing the process of adding DNS records when adding multiple sub-domains.
  3. Example of websites prefixing www: www.google.com, www.resellerclub.com and so on redirect the user to the www version of their website.

Non-WWW:

  1. These days having www as a prefix to your domain name isn’t compulsory and most websites these days have started using the non-www version. However, non-www domain names are known as naked domains.
  2. Though these domain names don’t have a technical edge, they prevent the owner from creating cookieless subdomains. The advantage with a non-www version the prefix is usually the representation of the page the user is currently viewing.
  3. Example of non-www websites: india.resellerclub.com lets you know this is an India specific website or developer.mozilla.org lets you know this website is for developers and so on.

Irrespective of whether you choose to prefix www to your domain name or not, the HTML pages are still interconnected and accessible over the internet. Although, neither are there direct implications on the website’s SEO nor is one version preferred over the other, however, if the users are redirected to either version, it might affect the overall SEO. A simple way to prevent this is by setting up the preferred preference on google.

Follow these steps to update your preference for Google Search Engine:

  1. Go to Google Webmasters Tools
  2. Select your website
  3. Click on the Settings icon on the extreme right side of the page
  4. In the drop-down menu, select ‘Site Settings’
  5. Here, set your ‘Preferred Domain’ (www or non-www)

Apart from setting your preference for Google, you can even redirect your www site to non-www and vice versa. We’ll be covering how to go about it for two servers, Apache (Linux) and IIS (Windows).

How to redirect www to non-www on Apache server: Linux Hosting

  1. For making changes on the Apache server, you’ll need to edit the .htaccess file
  2. Apache server is available on Linux powered Hosting and can be accessed via cPanel
  3. Login to your reseller account → Access cPanel of the domain you want to redirect
  4. In your cPanel, find File Manager
    www to non www
  5. Inside your File Manager, go to public_html and search for .htaccess file.
  6. If there isn’t a .htaccess file create a new one by clicking on ‘+ File’
  7. Insert the following code in the .htaccess file

    RewriteCond %{HTTP_HOST} ^www.yoursitedomain.com [NC]

    RewriteRule ^(.*)$ http://yoursitedomain.com/$1 [L,R=301]

How to redirect non-www to www on Apache server:

  1. Follow the above steps till step 6
  2. Instead of the code mentioned above in step 7, insert the following code in the .htaccess file should you want to redirect non-www to www

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

Should you already have a .htaccess file and ‘RewriteEngine On’ line in it, then all you need to do is insert the code below that line.

* If you have SSL certificate installed on your domain, in the code instead of ‘http’ write ‘https’.

Redirect www to non-www on IIS server: Windows Hosting

  1. For making changes on the IIS (Internet Information Services) server, you’ll need to edit the web.config file
  2. IIS server is available on Windows-powered Hosting and can be accessed via Plesk
  3. Login to your reseller account → Access Plesk panel of the domain you want to redirect
  4. In your Plesk panel, find File Manager

  5. Inside File Manager go to httpdocs and search for the web.config file.
  6. If there isn’t a web.config file create a new one by clicking on ‘+ New’
  7. Insert the following code in the web.config file. If you already have a web.config file then don’t mention the ‘configuration’ tag in your code.

    <configuration>
    <system.webServer>
    <rewrite>
    <rules>
    <rule name=”Redirect to non-www” stopProcessing=”true”>
    <match url=”.*” >
    <conditions>
    <add input=”{HTTP_HOST}” pattern=”^yoursitedomain\.com$” />
    </conditions>
    <action type=”Redirect” url=”http://yoursitedomain.com/{R:0}” redirectType=”Permanent” />
    </rule>
    </rules>
    </rewrite>
    </system.webServer>
    </configuration>

Redirect non-www to www on IIS server:

  1. Follow the above steps till step 6
  2. Instead of the code mentioned above in step 7, insert the following code in the web.config file should you want to redirect non-www to www

    <configuration>
    <system.webServer>
    <rewrite>
    <rules>
    <rule name=”Redirect to www” stopProcessing=”true”>
    <match url=”.*” />
    <conditions>
    <add input=”{HTTP_HOST}” pattern=”^yoursitedomain.com$” />
    </conditions>
    <action type=”Redirect” url=”http://www.yoursitedomain.com/{R:0}”
    redirectType=”Permanent” />
    </rule>
    </rules>
    </rewrite>
    </system.webServer>
    </configuration>

* If you have SSL certificate installed on your domain, in the code instead of ‘http’ write ‘https’.

If you have different URLs and still haven’t set the preferred URL, what are you waiting for? I suggest you follow this guide on how to redirect www to non-www and vice-versa today!

About H. Fatima

H. FatimaIn the days of yore, H. Fatima used to be an Engineer by profession and Writer by passion until she started pursuing full time writing. Her blog is a collection of short vignettes, stories and poems. She mostly writes what she deeply perceives and analyzes. It is her way of unwinding. Her interests include writing, reading (an avid reader), listening to various genres of music, volunteering and watching movies.

H. Fatima

H. Fatima

In the days of yore, H. Fatima used to be an Engineer by profession and Writer by passion until she started pursuing full time writing. Her blog is a collection of short vignettes, stories and poems. She mostly writes what she deeply perceives and analyzes. It is her way of unwinding. Her interests include writing, reading (an avid reader), listening to various genres of music, volunteering and watching movies.