How to redirect non-www URLs to www using htaccess?

@mit $ingh. July 09, 2016 Comments

In order to redirect all of the requests for yourdomain.com to www.yourdomain.com, you should set the appropriate rewrite rule. This can be done by adding the following lines at the beginning of the .htaccess file in your public_html folder (root directory):

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

From now on, when someone accesses http://yourdomain.com s/he will be redirected tohttp://www.yourdomain.com.

  • htaccess
  • www
  • redirect