How to redirect domain to www url with Nginx


Codever Logo

(P) Codever is an open source bookmarks and snippets manager for developers & co. See our How To guides to help you get started. Public bookmarks repos on Github ⭐🙏


This post presents the snippet from the Nginx configuration that redirects all request to https://www.codever.dev:

# redirect HTTP to www
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name bookmarks.dev www.codever.dev;

    return 301 https://www.bookmarks.dev$request_uri;
}

# redirect HTTPS to wwww
server {
    listen 443;
    server_name bookmarks.dev;

    return 301 https://www.bookmarks.dev$request_uri;
}

Note the missing www.codever.dev in the second server/server_name entry, to avoid an infinite loop

Lots of other good Nginx resources can be found if you search for the nginx tag on codingmarks: https://www.codever.dev?q=[nginx]

Octocat Source code for Codever.dev is available on Github - Star

Subscribe to our newsletter for more code resources and news

Adrian Matei (aka adixchen)

Adrian Matei (aka adixchen)
Life force expressing itself as a coding capable human being

routerLink with query params in Angular html template

routerLink with query params in Angular html template code snippet Continue reading