How to redirect non-www URLs to www?

If you want to redirect all requests of yourdomain.com to open www.yourdomain.com or if you are are using our CloudFlare cPanel tool and you need to open your website with www, then you should set a rewrite rule in the .htaccess file of your website. Follow these quick and simple steps to achieve that:

  • This will lead you to the File Manager‘s interface. Click on the button that says Settings in the upper right of the screen and make sure that the option “Show Hidden Files” is checked.

A screenshot of turning the cPanel Hidden Files display on

cPanel Hosting from WebhostFace
  • Find the .htaccess file, highlight it, click on the Edit button in the top toolbar, and Edit on the pop-up window.

  •  Add this code to the beginning of the file:
    • RewriteEngine On
    • RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
    • RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
  • You just need to substitute yourdomain.com with your actual domain name.
  • Click on Save Changes in the top right corner of the Code Editor window and from now your website will always open with www.

*Note: There are certain scripts and applications which require additional modifications in order for this to work. These are the so-called domain-dependent application like WordPress, Magento, PrestaShop which run only with a specific domain. Check the tutorials below to find out how to modify the primary domain for these scripts:

was this knowledge base article useful to you