Skip to content

Instantly share code, notes, and snippets.

@wei
Last active July 30, 2017 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wei/287704d376befae122e0ca201b21ee15 to your computer and use it in GitHub Desktop.
Save wei/287704d376befae122e0ca201b21ee15 to your computer and use it in GitHub Desktop.
Nginx WWW Redirection

Nginx WWW Redirection

Redirect non-www to www

For Single Domain:

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

For All Domains:

server {
    server_name "~^(?!www\.).*" ;
    return 301 $scheme://www.$host$request_uri;
}

Redirect www to non-www:

For Single Domain:

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

For All Domains:

server {
    server_name "~^www\.(.*)$";
    return 301 $scheme://$1$request_uri;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment