Redirect Subdomain 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 the domains have SSL certificates set up. First, configure the URL in the Ghost installation directory, /var/www/ghost:

ghost config url https://www.domain.tld

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'll need to redo the above steps for domain.tld.

note

If the server says you already have the SSL certificates set up, but they don't work, you can locate the SSL configuration files, delete them, and start over.

Locate the files

We'll need to change two of the four files in var/www/ghost/system/files/:

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

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

Redirect www.domain.tld to domain.tld

Update your www.domain.tld-ssl.conf so it looks like this, changing domain.tld to what yours is:

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.


Then update your www.domain.tld.conf so it looks like this, changing www.domain.tld. to what yours is:

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

Redirect domain.tld to www.domain.tld

Do the same thing as above, just switch the domain.tld to www.domain.tld and vice versa.

Restart Ghost

Reconfigure the domain to the one you want:

ghost config url https://domain.tld

Check the configuration:

ghost ls

Restart ghost to make the changes take into effect:

ghost restart

References

Last updated on