Question

How to Redirect All Requests to HTTPS-WWW in a single redirect [Nginx]

I am currently running a LEMP stack for my WordPress project. The domain I am using serves over HTTPS+WWW (https://www.example.com), which means all of the following requests are redirected to it:

Right now, all of these requests are being redirected to HTTPS+WWW as part of the redirect rules created by certbot during the setup. Below is my current server configuration.

server {
        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name example.com www.example.com;
        client_max_body_size 64m;

        location / {
                #try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }

        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt { log_not_found off; access_log off; allow all; }
        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
           expires max;
           log_not_found off;
        }

        location ~* ^/xmlrpc.php$ {
                return 403;
        }

        # WordPress: deny general stuff
        location ~* ^/(?:xmlrpc\.php|wp-links-opml\.php|wp-config\.php|wp-config-sample\.php|wp-comments-post\.php$
                deny all;
        }

    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;
        server_name example.com www.example.com;
    return 404; # managed by Certbot

    expires $expires;
}

Generally, a user would input HTTP + NON-WWW requests in the browser (like just example.com). When I checked via Varvy Redirect Mapper, I could see that the HTTP + NON-WWW request has two redirects. Here is a screenshot.

https://i.imgur.com/BEE9RQo.png

I want to resolve all the requests in just a single redirect to HTTPS-WWW (https://www.example.com). I have tried a couple of logics and also looked over to several other examples online but couldn’t find a proper solution.

Could someone please help?


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hi @dhananjaygbhardwaj,

In the second server block, you should replace https://$host$request_uri; with https://www.example.com$request_uri; like this:

server {
    if ($host = www.example.com) {
        return 301 https://www.example.com$request_uri;
    } # managed by Certbot


    if ($host = example.com) {
        return 301 https://www.example.com$request_uri;
    } # managed by Certbot

...
}