2 min read

Ghost www to root Redirect

Ghost www to root Redirect
Credits: Photo by Denys Nevozhai on Unsplash

I recently moved my blog from Medium to Ghost, something I wanted to do for a while. The blog is hosted using the root (apex) domain so I wanted to redirect www traffic to my root domain on my Ghost install as well - i.e. anyone visiting www.ledtechnica.com would be redirected to ledtechnica.com with a 301 permanent redirect status code.

While I expected this to be a relatively straightforward, finding all needed steps in a single location was not so capturing the steps here in case it is helpful to someone else:

  1. Change your DNS settings to create a CNAME record for www and point to root domain. I use AWS Route 53 and my entry looks as follows:

2. SSH into your Ghost instance and run the following commands (source):

# Set your blog URL to your secondary URL (in my case www)
ghost config url https://www.ledtechnica.com

# Get Ghost-CLI to generate an SSL setup for you
ghost setup nginx ssl

# Change your config back to your canonical domain
ghost config url https://ledtechnica.com

# Edit the nginx config files for your second domain to redirect to your canonical domain. In both files replace the content of the first location block with:(*)
return 301 https://ledtechnica.com$request_uri;

# Get nginx to verify your config
sudo nginx -t

# Reload nginx with your new config
sudo nginx -s reload

(*) I am using Ubuntu so had to make changes to the www config files (one for HTTP and one for HTTPS) under /etc/nginx/sites-available folder.

DigitalOcean Referral Badge
P.S. I am using Digital Ocean to host this blog and included my referral link above. Please feel free to use it to get $200 in credit to use over 60 days when you sign up.