How to Add 301 Redirect from www to non-www & Vice Versa using .htaccess

When you have a website created with new hosting, both www and non-www versions of your website domain resolve to your website.

The naked domain (http://example.com, without www) is the main top-level domain. All additional domain variants such as www.example.com are sub-domains of the main domain.

Personally, I find www sub-domain to be a relic of the past. Originally, www sub-domains were the standard URLs for hosting websites. Now, websites are hosted on a whole lot of other sub-domains. Quite often, the www sub-domain is completely unnecessary.

Having your website on both www and non-www domains is a big problem as far as search engine optimization is concerned. Any website should be accessible on only one website address.

You can choose to host your website on the nake domain, without www, and redirect the www sub-domain to the non-www domain.

For this, you need to set up website redirection. When properly setup, when someone tries to use the non-functional domain, the visitor get a message and then is redirected to the functional domain.

There are different types of website redirections possible by adding the redirection rules in .htaccess file of Apache-based web hosting.

  1. 301 Redirect: Permanently moved to another address
  2. 302 Redirect: Temporarily move to another address

How to 301 Redirect www to non-www via .htaccess

When setting up a website on the main domain, it is a good idea to redirect the non-www domain to the main one along with a 301 redirect (moved permanently) rule. This will send visitors to the correct address and also tell search engines that the website is present only on the main domain.

Important: The following method will only apply to Apache based web servers on Linux hosting. It does not apply to Windows hosting, which use IIS web servers.

Step 1: Find the .htaccess file

In the website’s public_html folder (the main folder) where your website files are located, you will find a file called .htaccess. It is a hidden file. If hidden files (starting with dot) are not visible, you will have to change the settings to show them.

If there is no .htaccess file, create one. Note that the file name should start with a dot.

Make sure to keep a backup of your original .htaccess file. If things go wrong, you can revert to the original code.

Step 2: Add the redirection code in .htaccess file.

To make the 301 redirect from www to non-www, you have to add the following code into your .htaccess file:

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

Make sure that you change example.com to your own website domain address. Also, if you want to use the https version, you can simply replace the target URL accordingly.

Here’s a better .htaccess code to redirect www to non-www with https (while using SSL).

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.(.)$ [NC]
RewriteRule ^(.)$ https://%1/$1 [R=301,L]
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

How to 301 Redirect non-www to www via .htaccess

If you are interested in using the www sub-domain instead, you can set up the 301 redirect rule to move all non-www requests to www. Here’s the .htaccess code for 301 redirect from non-www to www domain.

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

Here’s the code to redirect non-www to www with https (SSL).

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.(.)$ [NC]
RewriteRule ^(.)$ https://%1/$1 [R=301,L]
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]