How to remove www from your URL

I sometimes get asked how to remove www from your website’s URL – or add it and if it makes a difference or not. In this post, I’ll show you how you can enforce either a www or non-www URL by tweaking your .htaccess file (or nginx.conf if you’re running on an Nginx server).

Does using one or the other impact SEO?

You might be wondering if using one or the other will have an impact on your SEO. The answer is: no. It’s really just a matter of preference/esthetics. Just make sure you properly add the www and non-www domains in Google Search Console, as described here, to ensure Google can properly index your website.

Removing www from your domain name

If you prefer to market your website without the www prefix, you can add the following lines to your .htaccess file (Apache only):

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

Edit: As Thomas pointed out in the comments, it is not necessary to restart Apache after modifying the .htaccess file.

Note that Apache’s mod_rewrite module needs to be enabled. Otherwise, the above snippet won’t work.

Now, in Nginx this snippet is a bit different, but yields the exact same result when placed in the proper configuration file (which depends on your setup):

server {
 server_name www.example.com;
 return 301 http://example.com$request_uri;
}

Now just restart Nginx and you should be good to go!

Adding the www instead of removing it

To do the opposite of the previous section, add the following code to your .htaccess file:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^example.com$ 
RewriteRule (.*) http://www.example.com$1 [R=301]

And in Nginx, all it takes is this:

server {
 server_name example.com;
 return 301 http://www.example.com$request_uri;
}

That’s all there is to it!

But what about security?

As some of you pointed out in the comments (thank you for that!), there are some security concerns when you decide to use a non-www type url.

If you run a variety of different services on subdomains, you run the risk of sharing cookies between your main, non-www homepage and said service. This could potentially be bad if certain sensitive data is being stored in cookies that you don’t want shared with third-parties.

There are a few other concerns with cookies and non-www style urls, which you can read more about here.

Enjoyed this article?

If you'd like to receive my newsletter, you can sign up below. You'll be joining a friendly bunch that all get the latest news and insights, direct to their inbox.

Request a quote

Breathe new life into your site or get a new one off the ground. Get in touch to request a quote today.

Start a conversation about your project