Categories
nginx

Configure Domain With WWW or Non-WWW

When you browse websites, do you care if the site uses www in the URL or not? Most likely you probably didn’t even notice the difference. Some sites like to add www to their domain name (we do) and others prefer to leave it out. Either way, site owners make that choice and get it implemented so visitors don’t need to worry about it.

If you use nginx, you can redirect from one to the other very easily. For example, if you decide to redirect all visitors from www to the non www domain (naked domain), you can configure your nginx server block as shown below.

server {
  listen 80;
  server_name www.example.com;
  return 301 $scheme://example.com$request_uri;
}

server {
  listen 80;
  server_name example.com;
  root /usr/share/nginx/html; #make sure to match your server root 
  index index.php index.html index.htm;
  # you can add more below this

}

It’s simple! Give it a try on your new website. If you haven’t already, make sure you sign up for a new account through our link and get your $10 credit which is equal to two months of free service on the 512MB plan!

Do you prefer to use non www (naked) or www for your website?