Something we always have to do with new or old sites is add, for Florida SEO purposes, a redirect of non-www to www, or www to non-www (https://thebarton.org instead of https://www.thebarton.org). The reason being, Google sees thebarton.org and www.thebarton.org as two different sites and we’d get penalized for having duplicate content should someone put out a link to our site using one or the other.
The solution being, choose which one you want to have redirect – non-www to www or www to non-www. The choice is yours. It’s really just a personal preference for me to not use www in front of my domain.
non-www to www
RewriteEngine On # Redirect non-www to www RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]
If you already have redirects in place, just place this in your code somewhere:
# Redirect non-www to www RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]
The preceding two solutions are really generic. You can place them in your .htaccess without modification. How about www to non-www?
www to non-www
# Redirect www to non-ww RewriteCond %{HTTP_HOST} ^www\.(.*) [NC] RewriteRule ^/(.*)$ http://%1$1 [R=301,L]
Again, the preceding solution is generic and you can copy and paste it into your .htaccess file without modification. Unless of course you have some crazy kind of configuration. And if that’s the case, comment on this page and I’ll find a solution for you (as time permits).
Keep in mind, using %{HTTP_HOST} when you have different domains pointing to the same web root, will cause problems. In that instance, just use your primary domain.
What about Nginx?
Nginx is is almost as popular as Apache for serving web content. Here’s how to do it with Nginx:
server { server_name www.yourdomain.com; return 301 $scheme://yourdomain.com$request_uri; }
With any server side config file, you have to start the webs server.
service nginx restart
Conclusion
You’ve learned how to add, in your .htaccess file, a redirect for non-www to www domains. You’ve also learned how to redirect www to non-www. Furthermore, you’ve learned two reusable ways of doing what you wanted to do.
Hello,
Thanks for the tutorial.
My scenario is www to non-www so I tried;
# Redirect www to non-ww
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1$1 [R=301,L]
The rewrite occurs but with 2 trailing backslash like this
http://domain.com//
This generates a 404
Please advise how I can resolve, thanks again
You can do this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://domain.com$1 [R=301,L]
or
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^/(.*)$ http://%1$1 [R=301,L]
Thank you very much for this tutorial. My question is: How can I redirect ww.domainname.com to http://www.domainname.com? I would be very appreciated for help. Thanks in advance.
Hi Dave, so you’re asking about ww.domain.com to http://www.domain.com? 2 ww to 3 www? If so, it would be something like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ww\.(.*) [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,L]
Thank you very very much. That is exactly what I was looking for.