mercredi 26 février 2020

How to redirect to www. when a non-www URL is requested with nginx?

I have this code which captures all URLs:

server {
    listen 80;

    server_name _;

    return 301 https://www.$host$request_uri;
}

How would I make sure hat ANY URLs gets redirected to https://www. followed by heir respective $host and $request_uri? So his needs to work against all domains and not specific to just one.

So if the client tries http://example.com, it will redirect to https://www.example.com.

If the client tries https://example.com, it will redirect to https://www.example.com.

If the client tries https://www.example.com, then do nothing or redirect to https://www.example.com.

Note that I also have this server block which points to one of my domains:

server {
    listen 443 ssl;

    server_name somedomainname.com;

    ......
}



Aucun commentaire:

Enregistrer un commentaire