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

In that case you could deploy the following code in your .htaccess file to redirect www to non-www. 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:

Redirect non-www URLs to www

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 to http://www.yourdomain.com.

Redirect www URLs to non-www

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

Using this method is safe because it should not create any redirection loops nor it should interfere with other rewrite rules. This post is a follow-up to a similar adrticle on how to redirect non-www to www with .htaccess.

Share this :

Comments are closed.

Related post

Free Responsive Website templates

ThemeVina is Best site for free sharing and download Wordpress themes, Joomla templates, Responsive website templates and more HTML templates.

All template in Site are free. You can download and install for your site.

Contact us

  • Brand: Theme Vina
  • Email: admin@themevina.com
  • Website: www.themevina.com
  • Address: 367 Tran Hung Dao Street
  • Location: Thai Binh City, Thai Binh, Viet Nam

Newsletter