Photo by Jens Johnsson on Unsplash

How to Configure a 301 Redirect on Ghost Blog & DigitalOcean

Improve the SEO score of your Ghost blog with this tips

Guillaume Vincent
Guillaume Vincent

Table of Contents

In the last article, I explain how I set up my Ghost blog using DigitalOcean. Today, we are going how to configure 301 redirect to redirect traffic from www to non-www.

HTTP 301 redirect indicates the permanent moving of a web page from one location to another. It is important to configure it to not lose potential traffic and improve the SEO score of your blog.

In the following examples, replace replace.me domain name by your own domain

Configure a new Ghost URL

Log to your Ghost server through SSH and configure a new URL with these commands:

# sudo -i -u ghost-mgr
$ cd /var/www/ghost
$ ghost config url https://www.replace.me
$ ghost setup nginx ssl

A new Ghost URL has been configured. The Nginx SSL configuration and a fresh SSL certificate have been generated for https://www.replace.me.

Logout the ghost-mgr user session to continue:

$ exit

Configure HTTPS Redirection From WWW to Non-WWW Domain

The previous commands created a new Nginx configuration in /etc/nginx/sites-available/www.replace.me-ssl.conf . We need to modify it a bit to redirect the client to the same non-www URL domain:

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

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

    ssl_certificate /etc/letsencrypt/www.replace.me/fullchain.cer;
    ssl_certificate_key /etc/letsencrypt/www.replace.me/www.replace.me.key;
    include /etc/nginx/snippets/ssl-params.conf;

    client_max_body_size 50m;
    return 301 https://replace.me$request_uri;
}
/etc/nginx/sites-available/www.replace.me-ssl.conf

Configure HTTP Redirection From WWW to HTTPS Non-WWW Domain

The WWW HTTP configuration needs to be created from scratch. It redirects the visitors to HTTPS non-www domain URL:

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

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

    client_max_body_size 50m;
    return 301 https://replace.me$request_uri;
}
/etc/nginx/sites-available/www.replace.me.conf

The Final

Reload Nginx :

# nginx -s reload
Blogging

Guillaume Vincent Twitter

DevOps Engineer & AWS Certified Solution Architect. Cloud enthusiast and automation addict