Report this

What is the reason for this report?

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

Posted on September 20, 2020

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?



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!

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.

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

...
}

To achieve a single redirect for all non-https and non-www requests to https://www.example.com in Nginx, you can use a combination of a non-ssl server block to handle all http requests and a ssl server block to handle all non-www https requests. Here’s how you can modify your Nginx configuration:

1. Redirect All HTTP Requests

First, you’ll want to create a server block that listens on port 80 (HTTP) and redirects all requests to the https-www version. This covers both HTTP + NON-WWW and HTTP + WWW scenarios.

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://www.example.com$request_uri;
}

2. Redirect HTTPS non-www to HTTPS-www

Next, handle the HTTPS + NON-WWW scenario. You will need to set up a server block that listens on port 443 (HTTPS) for example.com and redirects to the www version.

server {
    listen 443 ssl http2;
    server_name example.com;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    return 301 https://www.example.com$request_uri;
}

3. Main Server Block for HTTPS-www

Finally, have your main server block handle the www and HTTPS version of your site. This is where your actual site configuration will be.

server {
    listen 443 ssl http2;
    server_name www.example.com;
    
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;
    
    # The rest of your configuration...
}

Summary

With this setup, you achieve the following:

Additional Notes

  • Ensure you reload or restart Nginx after making these changes (sudo systemctl reload nginx).
  • Test your configuration for syntax errors using sudo nginx -t.
  • After implementing these changes, use a redirect checker to confirm that all redirects work as expected with a single redirect.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.