skip to Main Content

I have an http/ https config in place which works but there is one issue our marketing team have asked about regarding no www in the URL. when users navigate to example.com the stanza redirects them to https://www.example.com fine but if they type in https://example.com the site loads but there is now www in the URL. This is apparently messing with SEO stats, etc.
I tried adding a redirect on the HTTPS server entry but it only ends up throwing a redirect loop.
I have pasted config below. I am not entirely sure why it does this I’d appreciate any thoughts.

server {
    listen 443 ssl http2;

    server_name .example.com;

    ############################################

    Tested redirect not working
    return 301 $scheme://www.example.com$requets_uri;

    ##################################################

    <ssl cert config here>

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-Content-Type-Options nosniff;
    add_header X-Frame-Options DENY;

    location / {
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass "http://127.0.0.1:3000";   
    }    
}

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

2

Answers


  1. Chosen as BEST ANSWER

    Ivan,

    I reread your comment, I hadn't actually implemented it how you'd suggested. I edited my config to reflect with a slight difference in that I am listening on ssl only for both as I have a separate stanza for 80 which redirects.

    This has resolved my issue, Thanks for the help.


  2. Try to use:

    # Main server
    server {
        listen 443 ssl http2;
        server_name www.example.com;
        ...
    }
    
    # Redirects all subdomains to www.example.com
    server {
        listen 80;
    
        # Uncomment if needed        
        # listen 443 ssl http2;
        # <ssl cert config here>
    
        server_name *.example.com;
        return 301 https://www.example.com$request_uri;    
    } 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search