Categories
QuickTip Web Development

How to Add www. – or Remove www. – From Your URL

This has been done many times by others, but just a quick tip on how to add or remove www. from your website’s address. This is helpful for URL canonicalization and user experience consistency. You can also achieve SEO-friendly canonicalization using rel="canonical". Oh boy, I’m getting too technical 🙂

Here’s the juicy part:

Add the following to the .htaccess file in your website’s root folder (often named public_html or www). If there’s not a file named .htaccess, you may create it. If you are creating the .htaccess file, set the permissions to 644. For security reasons, you don’t want others to be able to write to this file.

Note: These snippets are for http:// only, not secure https:// URLs.

Add www.


Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com/?(.+)?$
RewriteRule ^(.*)$ "http://www.example.com/$1" [R=301,L]

Remove www.


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

This will work with the majority of web hosts in my experience, but not all.

Additional resources

For more .htaccess information, and great follow-up reading on .htaccess, check out corz.org’s tutorial and second tutorial on some of the most-used .htaccess tricks.

By Zack Katz

Zack Katz is the founder of GravityKit and TrustedLogin. He lives in Leverett, Massachusetts with his wife Juniper.

5 replies on “How to Add www. – or Remove www. – From Your URL”

Hey, great article. What about Zeus types of servers where htaccess. files is not supported?

Comments are closed.