Ghost: Redirect www. to fix SEO issues

Ghost: Redirect www. to fix SEO issues
Photo by NisonCo PR and SEO / Unsplash

Learn how to forward all www requests to the non-www version of your Ghost site, thereby fixing SEO issues and ensuring any old links work.

Ghost is excellent at what it does, but it doesn't try to hold your hand or do anything without you asking it to. Redirecting www URLs to non-www is one of those things.

You can handle this in a number of ways, for example, if you're using a service like Cloudflare, you could add a page rule to cover this kind of redirect, however, doing this manually (using the Ghost CLI) is probably just as fast, and more easily validated once setup (and in future).

Moving from a platform that handles this automatically can mean it's not something you remember to do, which is why I'm writing this today: I woke up to an alert from Google because it was trying to index the www. version of each page, in addition to the canonical version. So let's fix this...

Request certificate for www.

First of all, we need to get a SSL/TLS certificate for the www. variant of your domain. Without this, you'll see a browser warning similar to the one below if you visit the www. version of your site.

cd to your Ghost install directory, then we'll change the URL and setup SSL:

cd /var/www/

ghost config url

ghost setup nginx ssl

You'll be prompted to enter your email address for the letsencrypt/ setup.

Once that has completed, make sure you change the URL back to the non-www version:

ghost config url

After all of the above, it's worth running a ghost restart and checking that the site is still working as expected.

Redirect www traffic

Now we can tell Nginx to forward any traffic to www. to the non-www version.

Using the Ghost commands previously will have created nginx config files for the www. sitess. You can confirm this by checkinf the files in /etc/nginx/sites-enabled:

ls /etc/nginx/sites-enabled/

We need to edit the two files starting with www. - e.g. & We'll make the same change to both files: find the block that starts with location / {:

    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;


Remove all of the lines in between { & }, and instead add: return 301$request_uri;

You should end up with a location block that looks like:

    location / {
        return 301$request_uri;
The $request_uri at the end means traffic will be forwarded to the non-www version of the page they were looking for. E.g. will forward to Handy!

Save changes to both files, and test that Nginx is happy with our work:

sudo nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If it is, reload Nginx:

sudo systemctl reload nginx

# or:

sudo nginx -s reload

That's it! You've forwarded any www. requests to your non-www URL. This will stop visitors getting an alarming warning page, and will help with your SEO when a search engine tries to hit www. versions of each of your pages.

If there's anything I've missed, please leave a comment below, or find me at @techbitsio.

Great! Next, complete checkout for full access to
Welcome back! You've successfully signed in
You've successfully subscribed to
Success! Your account is fully activated, you now have access to all content
Success! Your billing info has been updated
Your billing was not updated