How to Redirect WWW or Non-WWW through htaccess

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...
simple steps to redirect www and non-www through htaccess

Simple Steps to Solve WWW and Non-WWW Redirection through htaccess file

It’s very common questions from all users for all time. But, the concern of this question is very serious like putting all efforts in bucket A and trying to promote a bucket B.

Yes, If you will not solve this problem then it’s going to be the big blow to your business.




It’s better to take a firm decision about your business domain names like if you want to promote your business website with www or non-www.

Taking a decision about www and non-www redirection has multiple benefits like it will be standing as the brand name in the future, it will be a big advantage as search engine marketing, it will be a single uniform format for users and customers, and many more benefits are in a queue.

Let’s take this process in simple steps to solve www and non-www redirection issue through htaccess file in cpanel.

Step 1. Login to your cPanel.

Step 2. Locate the option File Manager to browse a number of files.

file manager option in cpanel

Step 3. Click on the Setting option from the top of the right corner.

setting option in file manage console in cpanel

Step 4. From a pop-up, called “Reference“. Select the radio button “show hidden files” from the root directory of a particular domain name.

Preference pop-up in file manager

Step 5. Discover .htaccess file from the list of files in root directory. Pick the .htaccess file and right click on it. Choose the option “Edit“.

root directory in file manager console

Step 6. Once you choose the option “Edit” option, then you will land on the text editor to edit .htaccess file. In order to solve www and non-www redirection issue, you should paste the below rewrite engine code in .htaccess file.

Note: We mentioned here two types of rewrite engine code, one for www to non-www redirection and another code for non-www to www redirection code. It’s up to you that which type of format you want to choose to promote your business website as the brand and for the search engine.

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

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

Step 7. Edit .htaccess file through above code and save it. That’s it. You are done with www and non-www redirection through htaccess.

Leave a Reply

Your email address will not be published. Required fields are marked *