Nginx http to https and www redirect

Hello,

I wanted to redirect http://novemgold.com to https://www.novemgold.com.

The https redirect works but I can not figure it out, how to redirect to www

Followed this tutorials:


without any success.

working nginx config for https redirect:

`server {

    # ports
    listen 80;
    listen [::]:80;

    # domain name
    server_name novemgold.com www.novemgold.com;

    # Redirect all non-https requests
    rewrite ^ https://novemgold.com$request_uri? permanent;
    #return 301 https://novemgold.com$request_uri;
}

server {

        listen 443 ssl;
        listen [::]:443 ssl;

        ssl_certificate         /etc/ssl/novemgold_com.pem;
        ssl_certificate_key    /etc/ssl/novemgold_com.key;

        server_name novemgold.com www.novemgold.com;
#       if ($host = 'novemgold.com') {
#               return 301 https://www.novemgold.com$request_uri;
#       }

        root /var/www/novemgold.com;
        index index.html index.htm index.php;

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

`
The commented out section of the config are mostly what I tried out

Also, activating this page rules results in the browser error “too many redirects”

Remove this rule and switch on the Always Use HTTPS feature in the SSL/TLS > Edge Certificates tab.

If you don’t want the redirect to apply to the whole zone, you can instead keep the Page Rule, but change the origin URL to start with http://, to avoid the redirect loop.

Hi,

page rule was not active.

Remove this rule and switch on the Always Use HTTPS feature in the SSL/TLS > Edge Certificates tab.

done that:

commented out this part:
# rewrite ^ https://novemgold.com$request_uri? permanent;

and activated this rule:
if ($host = 'novemgold.com') { return 301 https://www.novemgold.com$request_uri; }

getting again: browser error “too many redirects”

Sorry, I think I misread your OP. You should just set the Always Use HTTPS and your original page rule, that should take care of both redirects. The page rule will trigger first, and will redirect any example.com request to https://www.example.com. Requests with www. but not https:// will be handled by the Always Use HTTPS. So the combination should work, unless there are other redirects at your origin that contradict these.

I know nothing about Nginx, can’t help with that. I would think it possible to create a http or naked single redirect, as I do with .htaccess, you’d need to search this on StackOverflow or similar sites.

Removing the redirects:

server {
    # ports
    listen 80;
    listen [::]:80;
    # domain name
    server_name novemgold.com www.novemgold.com;
    # Redirect all non-https requests
    #rewrite ^ https://novemgold.com$request_uri? permanent;
    #return 301 https://novemgold.com$request_uri;
}

server {

        listen 443 ssl;
        listen [::]:443 ssl;

        ssl_certificate         /etc/ssl/novemgold_com.pem;
        ssl_certificate_key    /etc/ssl/novemgold_com.key;

        server_name novemgold.com www.novemgold.com;
#       if ($host = 'novemgold.com') {
#               return 301 https://www.novemgold.com$request_uri;
#       }

        root /var/www/novemgold.com;
        index index.html index.htm index.php;

and activating the page rule in cloudflare triggers the “too many redirects error”

Curl to http and https without (cloudflare page rule):

http://novemgold.com
--------------------
HTTP/1.1 301 Moved Permanently
 -> Location: https://novemgold.com/
HTTP/2 200


---------+++++-----------
https://novemgold.com
--------------------
HTTP/2 200