How to Redirect Non WWW to WWW Version of a Website and Vice Versa

If you have a website then you should know that your site has four different versions and you should choose your preferred version to avoid any issue related to SEO (Search Engine Optimization).

Here in this tutorial, you will learn how to redirect from non www to www version and why it is important.

A website has at least two site versions, non-www and www. And if you have an SSL certificate installed on your web server then two additional site versions will be included in your website.

Finally, you have four different versions of your website and search engines will see all these site versions as different websites. This might create some problems in case of SEO (Search Engine Optimization) and you may face site indexing and ranking issues in search engines.

Also if you have backlinks towards your domain then the SEO link juice might get divided between the different versions of your domain.

http://example.com
http://www.example.com
https://example.com
https://www.example.com

If you have a WordPress website or any custom website running on apache web server, you can easily redirect different versions of your domain to your preferred one.

redirect non www to www version website

To redirect your website url versions to your preferred one you need to use 301 permanent redirect. And to implement the redirect rules you should have a .htaccess file in your sites root directory. If there is no .htaccess file exist you can create a new one.

Website URL Redirection Non WWW to WWW and Vice Versa Using .htaccess

1. Redirect non www to www

If you want to redirect the non-www version of your website to www version then add the following lines in your website’s .htaccess file.

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

Note: Replace example.com with your own domain address.

2. Redirect www to non www

If you want to redirect the www version of your website to non-www version then add the following lines in your website’s .htaccess file.

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

Note: Replace example.com with your own domain address.

3. Redirect non www to www with https security

If you want to redirect all variation of your website URL version to www with https ssl security then add the following lines in your website’s .htaccess file.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

4. Redirect www to non www with https security

If you want to redirect all variation of your website URL version to non-www with https ssl security then add the following lines in your website’s .htaccess file.

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]

Site URL version redirection is one of the important steps to successfully achieve your site’s SEO goals. From the beginning, you should stick with one version so that your website doesn’t need to face any issue on search engines in the future.