Add or remove www

This short article is about adding or removing www from the domain in Nginx.

The examples below should be added somewhere in the HTTP block or just as one of the sites in the sites-available and sites-enabled folders.

First we have the configuration for removing www.

server {
    listen       80;
    server_name  www.domain.tld;
    return       301 http://domain.tld$request_uri;
}

Or we can use the reverse below to redirect to the domain with www (Beware that this section has not been tested).

server {
    listen       80;
    server_name  domain.tld;
    return       301 http://www.domain.tld$request_uri;
}

 

bb@morningtrain.dk'

Bjarne Bonde