4 min read

Redirect non-www to www URLs on Ghost

How to configure your Ghost Blog instance to redirect a non-www URL to the www address, and vice-versa.
Redirect non-www to www URLs on Ghost

Introduction

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

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 juancarrillo.co, 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.

Let's get hacking!

Prerequisites

How to Redirect non-www to www

  1. Log as the user that manages your Ghost instance. If your are using the Digital ocean 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 example.com domain, you would execute the following:

$ ghost config https://example.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.

4. Run ghost config url https://www.example.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 example.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: example.com.conf and example.com-ssl.conf.

Within the server block add a new line (do the same in both files) return 301 https://www.example.com$request_uri;:

# example.com.conf
server {
    listen 80;
    listen [::]:80;

    server_name example.com;

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

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

    ...
}
# example.com-ssl.conf
server {
    listen 443 ssl http2;
    listen [::]:443 http2;

    server_name example.com;

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

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

    ...
}

This directive tells Nginx to send any request for https://example.com to https://www.example.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

How to Redirect www to non-www

  1. Log as the user that manages your Ghost instance. If your are using the Digital ocean 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.example.com domain, you would execute the following:

$ ghost config https://www.example.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.

4. Run ghost config url https://example.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.example.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.example.com.conf and www.example.com-ssl.conf.

Within the server block add a new line (do the same in both files) return 301 https://example.com$request_uri;:

# www.example.com.conf
server {
    listen 80;
    listen [::]:80;

    server_name www.example.com;

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

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

    ...
}
# example.com-ssl.conf
server {
    listen 443 ssl http2;
    listen [::]:443 http2;

    server_name www.example.com;

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

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

    ...
}

This directive tells Nginx to send any request for https://www.example.com to https://example.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

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.juancarrillo.io to your main domain www.juancarrillo.co, 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!