Redirect www to non-www

Hey team, I’m new to Ghost - sorry for questions :slight_smile:

Seems, I just installed Ghost on https://onlinehikes.com

But, there is no redirect from e.g. https://www.onlinehikes.com/blog/how-to-enable-custom-thumbnails-in-youtube/ to non-www

I followed steps here Www to non-www on Digitalocean setup and here Solved: Need help on redirecting www. to non www. - GoDaddy Community

But, still https://www.onlinehikes.com/blog/how-to-enable-custom-thumbnails-in-youtube/ doesn’t redirect…

Hi Andrii.

You need to make some changes in nginx to make this work. There are a few different ways.

The easiest (but not the cleanest) way I’ve found is to get into the command line:

ghost config url https://www.onlinehikes.com
ghost setup nginx ssl

This creates the necessary SSL certificates and Nginx files to handle redirecting properly.

Run ghost config url https://onlinehikes.com to reset the domain setting.

Then you need to edit the two new Nginx files that are created (you’ll see them printed in the console) starting with www.onlinehikes.com (one will have -ssl in the name and one won’t).

Within the server block add a new line (do the same in both files):

return 301 https://onlinehikes.com$request_uri;

This will direct all traffic to your www domain, to your non-www domain.

Like I said, this may not be the cleanest way to do this but it’s the easiest I’ve discovered. Alternatively you could edit the existing Nginx files and create the SSL certificates from the command line. If anyone has any feedback on this, I’d love to hear it.

1 Like

What worked for me was:

  1. Configuring only the A records following this DigitalOcean tutorial
  2. For nginx I followed the Ghost docs (similar to what dan wrote). I pasted the redirect differently according to the docs instead.
  3. Reload nginx by running systemctl reload nginx and authenticating as the ghost user.
  4. To check that it worked, use curl -I https://www.yoursite.com and you should get a 301 Moved Permanently response.

Step 2 in detail

The nginx configuration files can be found in /etc/nginx/sites-available.

I pasted the return 301 https://yoursite.com$request_uri; line into the location block, like the documentation suggested.

So this:

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:2368;
    }

Becomes:

    location / {
        return 301 https://yoursite.com$request_uri;
    }

I went ahead to configure the http files to redirect to https as well.

1 Like