remove www from URL

How to remove www from your URL

How to remove www from your URL

February 03rd, 2017 – 12 Comments

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

Become a technical SEO expert with our Technical SEO 1 training! »

Technical SEO 1 training$ 179 - Buy now » Info

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.

Read more: ‘Ask Yoast: www and duplicate content’ »


12 Responses to How to remove www from your URL

  1. John-Pierre Cornelissen
    By John-Pierre Cornelissen on 14 February, 2017

    Why should you add both the www and non-www domains (as well as http/https in Google Search Console? If you use the .htaccess redirect to redirect all traffic to let’s say https://www.mydomain.nl then that’s all that Google will and need to visit. Or not?

    thx
    JP

    • Jimmy Comack
      By Jimmy Comack on 15 February, 2017

      Hi JP,

      By adding both to Google Search Console, you are able to tell Google what domain has your preference. If you decide you want the www version to be the ‘main’ domain name, Google will attempt to only list that version of your URL in the search results.

      By -not- adding the non-www version to GSC, you might run into issues where Google decides that the non-www version should be indexed because there are links pointing to the non-www version. This will then lead to search results where you’ll see a mixture of http://www.example.com and example.com.

      Adding both http and https to GSC should be done for an entirely different reason. By adding both version, you ensure that you can monitor all traffic coming to your website, no matter if visitors initially visited the http version instead of the https version. Obviously having a redirect in place in a necessity for (preferably) pointing visitors to https.

      I hope this answers your question.

      Cheers!

  2. Suanlian Tangpua
    By Suanlian Tangpua on 13 February, 2017

    Thank you very much.
    All the other links are working fine but when tested in google structured data testing tool my website text logo link is still pointed to the none www version. I preferred all my link to be in https://www.domain.com

    What will be the next step ….

    • Angelia Embler
      By Angelia Embler on 13 February, 2017

      We are sorry to hear this happened to your site. The logo URL may be listed incorrectly in a setting (theme or plugin) or may not have updated to the correct URL. Please review this knowledge base article: How can I get support?

  3. Bharat
    By Bharat on 6 February, 2017

    We need to make sure that we are doing proper 301 redirect while redirecting the domain from www to non www or vice versa.

  4. Sergio | Web
    By Sergio | Web on 5 February, 2017

    Hi Jimmy! Very interesting the article. I think you are a very good teacher. Interesting and simple article. Sorry my english …I must to improve. All the best from Spain!!

  5. Thomas
    By Thomas on 5 February, 2017

    When you’re modifying .htaccess files you don’t need to restart apache to see the change. You only have to do that if you edit the apache configuration file for the virtual host. Might want to correct that in the post.

    Also, I agree with the other commenters that in my experience it is preferable to use www for the reasons stated in that Johansen post even if there is no SEO reason to do so.

    • Jimmy Comack
      By Jimmy Comack on 14 February, 2017

      Hi Thomas! Thank you for pointing this out. I edited the article accordingly.

  6. Renato Alves
    By Renato Alves on 5 February, 2017

    Johansen published an article recently about it that I found illuminating.

    https://www.bjornjohansen.no/www-or-not

    It appears there is no problem from a SEO perspective, but it appears there is a possible security one, in case you use subdomains.

    Regardless, I still do prefer without the www.

    • Jimmy Comack
      By Jimmy Comack on 14 February, 2017

      Thank you for this. I added a small section to the post to highlight these concerns.

  7. Stijn Vogelss
    By Stijn Vogelss on 3 February, 2017

    Not sure it is a good idea to set your website to non-www. So far, I have seen little benefit, and some problems because of it.

  8. Dominik
    By Dominik on 3 February, 2017

    It’s not just “a matter of preference/esthetics”. Cookies on the “non-www” Domains will appear on all subdomains you might have.


Check out our must read articles about Analytics