Skip to main content

Redirect www to non www with htaccess and vice versa

Redirect www to non www

There is a popular method to perform this redirect:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteRule (.*)$1 [R=301,L]

Works fine, but you don't have to write domain every time you wan't to add new redirect. More friendly to use is this snippet:

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

We can this copy rule between other sites with different domains.

Redirect from non www to www

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

We use cookies on our website to enhance your user experience. We also use Google analytics and ads.

Click here to read more I've read it