Warning: Undefined array key 0 in /home/staging-yoast/staging-platform.yoast.com/versions/05aa62c2ec01538957086672ffa4a059780fd784/web/app/themes/yoast-com/single-post.php on line 46

How to remove www from your URL

At Yoast, we sometimes receive the question how to remove www from your website’s URL – or add it. 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):

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

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):

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

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:

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

And in Nginx, all it takes is this:

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

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 to be shared with third-parties.

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

Read more: Ask Yoast: www and duplicate content »

Coming up next!


12 Responses to How to remove www from your URL