Web Hosting

How to redirect non-www to www

Introduction

If you want to redirect your websites traffic from a non-www URL (domain.com) to a www domain (www.domain.com) then this guide is right for you. Here you’ll be able to set this up for both http (not secure) and https (secure).

Instructions

1.  Finding your .htaccess file
To start with, you need to locate your .htaccess file. Depending whether your using an FTP client or an online File Manager like cPanel, you will have to do different steps:

If you’re using an FTP client, simply go to your websites root directory (usually public_html) and open the .htaccess file. If you cannot see one, you’ll need to create a new file and name it “.htaccess”.

If you’re using cPanel, login to your cPanel account and open up the File Manager. Click on Settings in the top right and make sure the “Show Hidden Files (dotfiles)” checkbox is ticked. After that go to your websites root directory, select the .htaccess file and click on Edit.

2.  Editing your .htaccess file
Now you have your .htaccess file open to edit, simply paste the below code in there. Ensure that you don’t repeat “RewriteEngine On” in the file:

– If you would like to use http (not secure) then use the below code:

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

– If you would like to use https (secure) then use the below code:

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

Saving your .htaccess file and testing
After you have pasted the above code you can save the file. When you head to your website you will be forced to use www.

Conclusion

There you have it! All your website traffic should now be forced to www instead of non-www. If you need any further help please feel free to use the community forum and myself or another user will help you out!

Related Tutorials