DEV Community

Dries Deboosere
Dries Deboosere

Posted on • Originally published at driesdeboosere.dev on

How to redirect www to non-www with Nginx

When you have a website or web application behind a domain, then your users can access your website by the domain name and the www sub domain.

For example, this website is accessible through https://driesdeboosere.dev (without www sub domain) and https://www.driesdeboosere.dev (with www sub domain).

When a user goes to https://www.driesdeboosere.dev then that user will be redirected to https://driesdeboosere.dev (_without the www _).

To achieve this on a Nginx server you'll need to do the following:

Configure DNS

In order to set up this redirect, www.driesdeboosere.dev to driesdeboosere.dev or visa versa, you must have an A record for the domain (driesdeboosere.dev) and an A record for the sub domain (www.driesdeboosere.dev).

  • Create an A record for your domain name (e.g. driesdeboosere.dev) and set the IP address to the public IP address of your Nginx server (e.g. 165.22.205.49).
  • Next, add another A record for your sub domain (e.g. www.driesdeboosere.dev) and specify the same IP address.

It should look something like this:

Dns records

Now your servers is accessible by the www and non-www domain, but we still need to configure the redirect. That's the next step.

Configure Nginx redirect

We can do two redirects, a 301 or a 302 redirect. Learn about the difference between the two.

In our case, we want a permanent 301 redirect, so that all traffic through www.driesdeboosere.dev redirects to driesdeboosere.dev. We'll setup a new Nginx server block that points to your original server block.

  • Login to your server
  • Enter following command:
sudo nano /etc/nginx/conf.d/redirect.conf
Enter fullscreen mode Exit fullscreen mode

Depending on which direction you want to redirect, use one of the two options below.

Option 1: Redirect www to non-www

If you want to redirect your users from www to non-www domain, insert following configuration in the editor:

server {
    server_name www.driesdeboosere.dev;
    return 301 $scheme://driesdeboosere.dev$request_uri;
}

Enter fullscreen mode Exit fullscreen mode

Save and exit.

To make the changes take effect, restart Nginx by entering following command:

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Test your redirect in the browser; entering www.driesdeboosere.dev will redirect to driesdeboosere.dev.

We can test this with a curl command, enter following in PowerShell:

curl -I www.driesdeboosere.dev
Enter fullscreen mode Exit fullscreen mode

You should get a 301 Moved Permanently response like this:

HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0 (Ubuntu)
Date: Thu, 12 Aug 2021 13:04:32 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
Location: https://driesdeboosere.dev/
Enter fullscreen mode Exit fullscreen mode

Option 2: Redirect non-www to www

If you want to redirect your users from non-www to www domain, insert following configuration in the editor:

server {
    server_name driesdeboosere.dev;
    return 301 $scheme://www.driesdeboosere.dev$request_uri;
}

Enter fullscreen mode Exit fullscreen mode

Save and exit.

To make the changes take effect, restart Nginx by entering following command:

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Test your redirect in the browser; entering driesdeboosere.dev will redirect to www.driesdeboosere.dev.

We can test this with a curl command, enter following in PowerShell:

curl -I driesdeboosere.dev
Enter fullscreen mode Exit fullscreen mode

You should get a 301 Moved Permanently response like this:

HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0 (Ubuntu)
Date: Thu, 12 Aug 2021 13:04:32 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
Location: https://www.driesdeboosere.dev/
Enter fullscreen mode Exit fullscreen mode

Conclusion

That’s it! Your Nginx permanent redirect is now configured properly, and your users will be able to access your web server via your non-www and www domain.

Difference between a permanent and temporary redirect

301 redirect (Permanent)

A 301 redirect is an permanent redirect. This way we inform search engines and browsers that a page is permanent redirected to a new address. The old address will be removed from the search engines and be replaced by the new address.

302 redirect (Temporary)

A 302 redirect is an temporary redirect. This way we inform search engines and browsers that a page is temporary redirected to a new address.

Top comments (0)