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):
RewriteEngine On RewriteCond %{HTTP_HOST} ^www.example.com$ [NC] RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
Restart Apache and check that you get redirected to the non-www version when using the www prefixed URL.
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!