In this short tutorial, I’m going to present you how to setup http to https as well as www to non-www (or vise-versa) redirections on your ghost blog. If you have not already done so, first install ghost on your server. You can do this by following their official tutorial here.

Why is this important for the referencing of your blog?

Google announced in August 2014 that HTTPS pages would receive a small SEO bonus. Although several SEO agencies have indicated that this is not a decisive ranking factor, it can be the tiebreaker that will allow you to be ranked higher than your competitor. Redirecting the http traffic to https will ensure all your visitors use a secure protocol while accessing your website.

Your website/blog should only be accessible from one address. You can either use a subdomain (blog.example.com) or choose between using www or non-www URLs. If you keep both URLs accessible, google will see your www and non-www links as different and duplicate your content (which will ultimately lower your ranking). To ensure everyone uses the same URL, you will need to redirect your users to the correct one if they come from a non-standard location.

Should you use www or non-www urls?

From a SEO perspective, there are absolutely no difference between www and non-www URLs. It's up to you to choose. Once you decided, it is important to NOT change your site URL to add or remove www from it or it will reset your ranking on all your articles and it will feel like you're just starting a new blog.

Redirecting HTTP to HTTPS

  1. We will check that the current installation of your blog has SSL enabled. You can do this by typing the following command:
ghost status

which will return something like this:

"ghost status"

Make sure that the URL has "https" in it. Next, we will edit the nginx configuration to redirect the user to the correct URL.

2.  By default, the Ghost CLI adds the nginx configuration files to /etc/nginx/sites-available. To setup the redirection, we will modify the file example.com.conf located in this folder (you must replace example.com with the URL of your blog). Warning: do not modify example.com-ssl.conf, you only want to edit example.com.conf

In this file, remove everything inside the location / { } block and append the following line to it:

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

Again, you need to replace example.com with your domain name. Your file should look like this:

server {
    listen 80;
    listen [::]:80;

    server_name example.com;
    root /var/www/example/system/nginx-root; # Used for acme.sh SSL verification (https://acme.sh)

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

    location ~ /.well-known {
        allow all;
    }

    client_max_body_size 50m;
}
/etc/nginx/sites-available/example.com.conf

3.  Restart nginx to save the changes.

sudo systemctl restart nginx

Redirecting www URLs to non-www.

Verify that your www and non-www DNS records point to the same IP address. You can use a CNAME to make sure they do.

  1. Generate a SSL certificate for your www or non-www URL. To do this, we are going to use the acme.sh tool, that the Ghost CLI uses by default.
sudo /etc/letsencrypt/acme.sh --issue --home /etc/letsencrypt \
--domain example.com --domain www.example.com \
--webroot /var/www/example/system/nginx-root \
--reloadcmd "nginx -s reload" --accountemail email@example.com

On the second line, replace example.com and www.example.com with your actual domain name, on the third line, replace /var/www/example/ with the location of your ghost installation and on the last line replace email@example.com with your email address.

2.  Edit the nginx configuration

Create a new file in /etc/nginx/sites-available called www.example.com.conf and append the following content to it:

server {
    #in the following lines, replace example.com with your domain name.
    server_name www.example.com;  # the URL you want to redirect from
    root /var/www/example/system/nginx-root; # replace /var/www/example with the location of your ghost installation

    location / {
        return 301 https://example.com$request_uri; # the URL you want to redirect to 
    }
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    #in the following lines, replace example.com with your domain name.
    server_name www.example.com; # the URL you want to redirect from
    root /var/www/example/system/nginx-root; # replace /var/www/example with the location of your ghost installation

    ssl_certificate /etc/letsencrypt/example.com/fullchain.cer;
    ssl_certificate_key /etc/letsencrypt/example.com/example.com.key;
    include /etc/nginx/snippets/ssl-params.conf;

    location / {
        return 301 https://example.com$request_uri; # the URL you want to redirect to
    }
}
/etc/nginx/sites-available/www.example.com.conf

Finally, enable the configuration by creating a symbolic link:

sudo ln -s /etc/nginx/sites-available/www.example.com.conf /etc/nginx/sites-enabled

3.   Restart nginx to save the changes.

sudo systemctl restart nginx


Voilà! You can now check all the URLs to ensure that the redirections are properly working.