Redirect non-www to www URLs in Ghost

How to configure your Ghost instance to redirect a non-www URL to the www address and vice-versa.

Great! You are done setting up your Ghost Blog on your domain. Let's say, www.juanca.org.

But you know that most people will type your website URL directly (if they don’t use a search engine) on the address bar of their web browsers without the "www" subdomain.

If I type juanca.org, it should not be a problem for landing to the website, right?

You may run your Ghost blog on the www or non-www address as a personal preference, but to prevent redundant hostnames, it's recommended to choose one style and be consistent throughout your site.

Whichever address you choose, you should make a permanent HTTP 301 redirect from the other address.

This guide describes how to configure your Ghost Blog instance to redirect a non-www URL to the www address, and vice-versa. Choose whatever you see fit for your website.

Let's get hacking!

Prerequisites

  • You have a Ghost instance running on a VPS. This can be from any provider, but for this guide, we'll use the Ghost Hosting 1-Click App from DigitalOcean. If you don't have a DigitalOcean Account, you can start by using my referral link and get $100 in credit valid for two months.
  • You have a domain name such as yourdomain.com whose DNS A records for @ (the naked domain, without www) and www are pointing to the same IP address of the Ghost instance.
  • Knowing how to login to your VPS using ssh or, if you are using DigitalOcean, you can use the Droplet Console.

How to redirect non-www to www

  1. Log as the user that manages your Ghost instance. If your are using the DigitalOcean 1-Click App, it's gonna be ghost-mgr. Run the following command on your VPS console:
sudo -i -u ghost-mgr

2. Locate your Ghost instance, which is typically located in /var/www/ghost. Change directory to that location:

cd /var/www/ghost

3. Configure your non-www domain on Ghost and setup the SSL certificate for that domain. For the yourdomain.com domain, you would execute the following:

ghost config url https://yourdomain.com;
ghost setup nginx ssl

Nginx will ask for an email for the SSL certificate. Add the one that you normally use for anything public, like your git commits, i.e. [email protected].

4. Run ghost config url https://www.yourdomain.com to reset the domain setting.

5. Locate your virtual host configuration file, which is typically located in /etc/nginx/sites-available. Change directory to that location:

cd /etc/nginx/sites-available

6. Edit the two new Nginx files that are created (you’ll see them printed in the console) starting with yourdomain.com (one will have -ssl in the name and one won’t). Use a text editor like vim or nano.

These files will be named something like: yourdomain.com.conf and yourdomain.com-ssl.conf.

Within the server block add a new line with the following: return 301 https://www.yourdomain.com$request_uri;. Do the same in both files:

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

    server_name yourdomain.com;

    # THIS IS THE NEW LINE:
    return 301 https://www.yourdomain.com$request_uri;
    ##############################################

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

    ...
}

yourdomain.com.conf file

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

    server_name yourdomain.com;

    # THIS IS THE NEW LINE:
    return 301 https://www.yourdomain.com$request_uri;
    ##############################################

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

    ...
}

yourdomain.com-ssl.conf file

This directive tells Nginx to send any request for https://yourdomain.com to https://www.yourdomain.com with HTTP redirect code 301.

7. Restart Nginx to apply the change. For Ubuntu or Debian systems use the command:

sudo service nginx restart

8. Restart Ghost:

cd /var/www/ghost;
ghost restart

How to redirect www to non-www

  1. Log as the user that manages your Ghost instance. If your are using the DigitalOcean 1-Click App, it's gonna be ghost-mgr. Run the following command on your VPS console:
sudo -i -u ghost-mgr

2. Locate your Ghost instance, which is typically located in /var/www/ghost. Change directory to that location:

cd /var/www/ghost

3. Configure your www domain on Ghost and setup the SSL certificate for that domain. For the www.yourdomain.com domain, you would execute the following:

ghost config url https://www.yourdomain.com;
ghost setup nginx ssl

Nginx will ask for an email for the SSL certificate. Add the one that you normally use for anything public, like your git commits, i.e. [email protected].

4. Run ghost config url https://yourdomain.com to reset the domain setting.

5. Locate your virtual host configuration file, which is typically located in /etc/nginx/sites-available. Change directory to that location:

cd /etc/nginx/sites-available

6. Edit the two new Nginx files that are created (you’ll see them printed in the console) starting with www.yourdomain.com (one will have -ssl in the name and one won’t). Use a text editor like vim or nano.

These files will be named something like: www.yourdomain.com.conf and www.yourdomain.com-ssl.conf.

Within the server block add a new line with the following: return 301 https://yourdomain.com$request_uri;. Do the same in both files:

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

    server_name www.yourdomain.com;

    # THIS IS THE NEW LINE:
    return 301 https://yourdomain.com$request_uri;
    ##############################################

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

    ...
}

www.yourdomain.com.conf file

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

    server_name www.yourdomain.com;

    # THIS IS THE NEW LINE:
    return 301 https://yourdomain.com$request_uri;
    ##############################################

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

    ...
}

www.yourdomain.com-ssl.conf file

This directive tells Nginx to send any request for https://www.yourdomain.com to https://yourdomain.com with HTTP redirect code 301.

7. Restart Nginx to apply the change. For Ubuntu or Debian systems use the command:

sudo service nginx restart

8. Restart Ghost:

cd /var/www/ghost;
ghost restart

Conclusion

That's it! Now you are ready to go and redirecting your blog correctly don't matter what people type on their web address bar.

An important note that I want to make is that if you also need to redirect another domain like www.juanca.dev to your main domain www.juanca.org, this guide can help you too! Just remember to replace the correspondent domain names and follow the steps from above.

Don't forget that all domain names that you want to redirect to your main domain should have DNS A records for @ and www pointing to the IP address of the Ghost instance!

Happy hacking!