Removing www From Site URL Tutorial

Learn how to remove the www prefix from your website and domain URL and explore top methods of adding or removing it to enforce the redirect

Updated on September 11, 2022
Removing www From Site URL Tutorial

Removing www tutorial: Learn how to remove www prefix from your website’s domain URL? Or add it. In this post, I’ll show you how you can enforce a www or non-www URL by tweaking your .htaccess file.

Removing www From Site URL

Does using one or the other impact SEO?

You might wonder if using one or the other will impact your SEO. The answer is: no. It’s just a matter of preference/aesthetics. Ensure you correctly add the www and non-www domains in Google Search Console, as described here, to ensure Google can properly index your website.

remove www prefix domain
Removing www From Site URL 2

Removing prefix www from your domain name

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

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

Edit: As Thomas pointed out in the comments, it is unnecessary to restart Apache after modifying the .htaccess file and removing the www from the 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 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 to remove the www from the domain URL !

But what about security?

Removing www From Site URL 1 remove www prefix domain
Removing www From Site URL

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 be bad if certain sensitive data is being stored in cookies that you don’t want to share with third parties.

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

CDN Cloudflare To Remove or Add www Prefix From Domain URL

Cloudflare dashboard click on the Rules tab

If you plan to set up a CDN like Cloudflare to improve website speed and performance, you can also use it to redirect non-www to www domains. After creating an account, log in and follow the instructions below: In the Cloudflare dashboard, click the Rules tab.

Select 301 – Permanent Redirect as the status code

Press the Create Page Rule button. Enter your current website URL without www and set the page rule to redirect url. Select Status Code 301 – Permanent Redirect. Enter the website URL starting with www and click Save and Publish. Wait for the redirect to go live.