How to Redirect to www or non-www using httaccess Print

  • 0

Using .htaccess to Redirect to www or non-www

Simply put the following lines of code into your main, root .htaccess file.
In both cases, just change out domain.com to your own hostname.

Redirect to www

 

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
</IfModule>

Redirect to non-www

 

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
</IfModule>

Was this answer helpful?

« Back