Home

Remove the "www" from a URL with Nginx

For SEO purposes, it's best to choose between www and no www. See how to do it with Nginx.

If you don't want to include www as your primary domain, it's nice to still allow traffic with and without the www in the domain name. For example, if your primary domain is example.com, you'd also want to allow traffic to www.example.com.

It is ideal for SEO (search engine optimization) purposes that you choose one (with or without www) and stick with it by redirecting traffic from the other domain to your main domain.

With Nginx as your web server, the proper way of doing so is to perform a 301 redirect in a separate server directive. Use the following in addition to your main server directive.

server {
server_name www.yourdomain.com;
return 301 $scheme://yourdomain.com$request_uri;
}

Note: It's okay to have multiple directives in one file, but it's good to keep the directives separate from one another.

In this case, we're getting rid of the www and considering yourdomain.com to be your main domain. You could just as easily do the reverse, like so:

server {
server_name yourdomain.com;
return 301 $scheme://www.yourdomain.com$request_uri;
}

Let's Connect

Keep Reading

Remove HTML Extension And Trailing Slash In Nginx Config

When you have a static site, sometimes you want to get rid of the HTML extensions and those pesky trailing slashes. Here's how I've done it.

Jul 22, 2015

Being "Busy" Isn't An Excuse

We're all busy. All the time. You aren't busier than anyone else. You just need to learn how to communicate why you can't (or couldn't) do something.

Mar 21, 2016

Parse a Web Page and Post to Slack Using Ruby

When another website is doing the heavy-lifting updating some content you want to keep up on, but there's no good integration with Slack, you can parse it yourself and post directly to a Slack channel.

Feb 24, 2016