How to Force www or non-www in htaccess

 

In some case, many clients request to force either www or non-www version of their site to display their visitor's browser. For example, you can have www.yourdomain.com or simply yourdomain.comdisplay in an address bar. There is common way to forcing one format is better for search engine optimization. So here we discuss how to force www or non-www in your .htaccess file. You can access the .htaccess file through the cPanel file manager.

 

1. Log into your cPanel.

2. Click on My account-> Go to C panel

3. Find File category and Click File manager

file manager in c panel

 

4. You can see all your project folder appear here. Open any one project folder which you want to perform this task.you can see all files appear here. you can also see .htaccess file appear here. If you don't want to see .htaccessfile in your project folder then click on Settings top of the corner.

file manager in c panel

 

5. Choose the document root for thedomain you are working with and be sure the checkbox next to Show Hidden Files is checked. Click the Save button.

6. Look for the  .htaccess file and right click on it. Thisbrings up a menu. Find and click on the Edit option.

edit option in .htaccess

 

7. Now, you are in text editor.

  • If you want www version of your site. For example you want to display www.yourdomain.com in an address bar thanadd below code in your .htaccess file and save file.

Note: Replace ‘yourdomain.com' with your actual domain name

            <pre>

            RewriteEngine on

            RewriteCond %{HTTP_HOST} ^ yourdomain.com [NC]

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

            </pre>

If you want non-wwwversion of your site. For example you want to display  yourdomain.com in an address bar than add below code in your .htaccess file and save file.

Note: Replace ‘yourdomain.com' with your actual domain name

           <pre>

             RewriteEngine on

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

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

           </pre>

8. Now you can type your domain in browser it can display as you can set it in .htaccess file.

I hope this post is helpful for you. Ifyou have any issue than place the comment below. We are happy to connect withyou and solved your issue. If you have query than contact our experienced developer at Lathiya Solutions Surat, who can ready to help you.