Here is a 301 redirect syntax that does not loop, and does not affect your subdomains either.


If you’re new to 301 redirects, the first thing you want to do is create or edit an existing .htaccess file, which should be in your public_html or web root folder.  Add the following lines, substituting your actual domain name instead of the ‘yourdomain’ and ‘com’ we list here.

This one will take you from non-www to www:

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

Save those changes, and you should now see redirects from non-www to www without looping and without losing your url structure.

If you wanted to redirect the domain EXCEPT for a certain folder, you can use the following;

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(.*)\.yourdomain\.com$ [NC] 

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

In the above example, the folder that we are NOT wanting to redirect is the “/blog” folder.

in HTACCESS