Skip to main content

How to redirect subdomains (www to non-www) on Ghost blog

We can redirect subdomains by changing the Nginx configuration files. We can:

  • Redirect www.domain.tld to domain.tld
  • Redirect domain.tld to www.domain.tld

The easiest way to do this is with Cloudflare's Page Rules, but we have a limited number of free page rules, so we can save them by doing these subdomain redirects on the server.

Set up SSL

We need to set up SSL certificaates for each domain.

  1. Switch to the ghost-mgr user.
    sudo -i -u ghost-mgr
  2. Move to the Ghost installation directory
    cd /var/www/ghost
  3. Configure the url:
    ghost config url https://www.domain.tld
  4. Then, set up the SSL Certificates.
    ghost setup nginx ssl

If you started your droplet with https://domain.tld, then the SSL for domain.tld was arleady installed as part of the 1-Click installation process. If you are moving from otherdomain.tld to domain.tld, then you need to redo the above steps for domain.tld.

note

If the server says you already have the SSL certificates set up, but they do not work, then locate the Nginx and SSL configuration files, delete them, and start over.

Locate the Configuration Files

The four files that we are concerned about are in /var/www/ghost/system/files/ or /etc/nginx/sites-available:

  • www.domain.tld-ssl.conf
  • www.domain.tld.conf
  • domain.tld-ssl.conf
  • domain.tld.conf

You can update these files by the command line using either the nano or vim editors, but you can also use FileZilla. In the following instructions, we use the nano editor.

To delete a configuration file, use the following command:

sudo rm -rf www.domain.tld.conf

Redirect www.domain.tld to domain.tld

  1. Locate the configuration files.
    cd /var/www/ghost/system/files
  2. Verify that the files are there. Type ls and press enter. You should see at least these four files. If not, then something went wrong when setting up SSL:
    • domain.tld.conf
    • domain.tld-ssl.conf
    • www.domain.tld.conf
    • www.domain.tld-ssl.conf
  3. Open the editor for www.domain.tld-ssl.conf:
    nano www.domain.tld-ssl.conf
  4. Add # to the beginning of lines 12-19 and add line 26, replacing domain.tld with your domain. The result will look like this:
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;

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

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

#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;
#
#}

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

client_max_body_size 50m;
return 301 https://domain.tld$request_uri;
}

We need to keep the stuff about the ssl certificate in order for the certificates to renew.

  1. Save your changes. Press Ctrl/Cmd + O on your keyboard. Press Enter. Then Press Ctrl/Cmd + X to exit.

  2. We need to repeat the process for www.domain.tld.conf. Open the editor for www.domain.tld.conf:

    nano www.domain.tld.conf
  3. Add # to the beginning of lines 8-15 and add line 22, replacing domain.tld with your domain. The result will look like this:

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

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

#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;
#
#}

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

client_max_body_size 50m;
return 301 https://domain.tld$request_uri;
}
  1. Save your changes. Press Ctrl/Cmd + O on your keyboard. Press Enter. Then Press Ctrl/Cmd + X to exit.

Restart Nginx and Ghost

After you are finished editing the Nginx configurations, you need to restart Ghost make the changes take into effect.

  1. Reconfigure the domain to the one you want:
    ghost config url https://domain.tld
  2. Check the configuration:
    ghost ls
  3. Check the Nginx configuration. If you created configuration files with domain.tld instead of your actual domain, then you may have errors. Visit /etc/nginx/sites-available and /etc/nginx/sites-enabled and delete the configuration files for domain.tld using sudo rm -rf domain.tld-ssl.conf and sudo rm -rf domain.tld.conf:
    nginx -t
  4. Restart Nginx to make the new Nginx configurations take into effect:
    sudo systemctl restart nginx
  5. Restart Ghost to make the new Ghost changes take into effect:
    ghost restart

Redirect domain.tld to www.domain.tld

Repeat the steps above, switching domain.tld with www.domain.tld and vice versa.

References