# Redirect www to non-www

**URL:** https://forum.ghost.org/t/redirect-www-to-non-www/20494
**Category:** Installation
**Created:** [March 1, 2021, 11:14pm UTC](https://forum.ghost.org/t/redirect-www-to-non-www/20494 "2021-03-01T23:14:45Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![andrii](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/andrii/32/11786_2.png) [@andrii](https://forum.ghost.org/u/andrii)
#### Post date: [March 1, 2021, 11:14pm UTC](https://forum.ghost.org/t/redirect-www-to-non-www/20494/1 "2021-03-01T23:14:45Z")

</div>

Hey team, I’m new to Ghost - sorry for questions 🙂

Seems, I just installed Ghost on [https://onlinehikes.com](https://onlinehikes.com)

But, there is no redirect from e.g. [https://www.onlinehikes.com/blog/how-to-enable-custom-thumbnails-in-youtube/](https://www.onlinehikes.com/blog/how-to-enable-custom-thumbnails-in-youtube/) to non-www

I followed steps here [Www to non-www on Digitalocean setup](https://forum.ghost.org/t/www-to-non-www-on-digitalocean-setup/4078) and here [GoDaddy Community](https://www.godaddy.com/community/Managing-Domains/Need-help-on-redirecting-www-to-non-www/td-p/45214)

But, still [https://www.onlinehikes.com/blog/how-to-enable-custom-thumbnails-in-youtube/](https://www.onlinehikes.com/blog/how-to-enable-custom-thumbnails-in-youtube/) doesn’t redirect…

---

<div class="post-metadata">

### Author: ![dan](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/dan/32/7545_2.png) [@dan](https://forum.ghost.org/u/dan)
#### Post date: [March 2, 2021, 4:31pm UTC](https://forum.ghost.org/t/redirect-www-to-non-www/20494/2 "2021-03-02T16:31:11Z")

</div>

Hi Andrii.

You need to make some changes in nginx to make this work. There are a few different ways.

The easiest (but not the cleanest) way I’ve found is to get into the command line:

`ghost config url https://www.onlinehikes.com`  
`ghost setup nginx ssl`

This creates the necessary SSL certificates and Nginx files to handle redirecting properly.

Run `ghost config url https://onlinehikes.com` to reset the domain setting.

Then you need to edit the two new Nginx files that are created (you’ll see them printed in the console) starting with `www.onlinehikes.com` (one will have `-ssl` in the name and one won’t).

Within the `server` block add a new line (do the same in both files):

`return 301 https://onlinehikes.com$request_uri;`

This will direct all traffic to your www domain, to your non-www domain.

Like I said, this may not be the cleanest way to do this but it’s the easiest I’ve discovered. Alternatively you could edit the existing Nginx files and create the SSL certificates from the command line. If anyone has any feedback on this, I’d love to hear it.

---

<div class="post-metadata">

### Author: ![adventgineering](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/adventgineering/32/18252_2.png) [@adventgineering](https://forum.ghost.org/u/adventgineering)
#### Post date: [July 19, 2021, 12:48pm UTC](https://forum.ghost.org/t/redirect-www-to-non-www/20494/3 "2021-07-19T12:48:54Z")

</div>

What worked for me was:

1. Configuring only the A records [following this DigitalOcean tutorial](https://www.digitalocean.com/community/tutorials/how-to-redirect-www-to-non-www-with-nginx-on-ubuntu-14-04)
2. For `nginx` I [followed the Ghost docs](https://ghost.org/docs/ghost-cli/#ssl) (similar to what dan wrote). I pasted the redirect differently according to the docs instead.
3. Reload `nginx` by running `systemctl reload nginx` and authenticating as the ghost user.
4. To check that it worked, use `curl -I https://www.yoursite.com` and you should get a `301 Moved Permanently` response.

### Step 2 in detail

The `nginx` configuration files can be found in `/etc/nginx/sites-available`.

I pasted the `return 301 https://yoursite.com$request_uri;` line into the `location` block, like the documentation suggested.

So this:

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

```

Becomes:

```auto
    location / {
        return 301 https://yoursite.com$request_uri;
    }

```

I went ahead to configure the `http` files to redirect to `https` as well.

---

<div class="post-metadata">

### Author: ![ericleaf](https://avatars.discourse-cdn.com/v4/letter/e/7feea3/32.png) [@ericleaf](https://forum.ghost.org/u/ericleaf)
#### Post date: [August 8, 2023, 4:39pm UTC](https://forum.ghost.org/t/redirect-www-to-non-www/20494/4 "2023-08-08T16:39:26Z")

</div>

Registered just to say this: Thank you Dan. This helped me very much!

PS: I used the version in sites-available and not the sites-enabled folder. That worked for me.

---

<div class="post-metadata">

### Author: ![rik](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/rik/32/27616_2.png) [@rik](https://forum.ghost.org/u/rik)
#### Post date: [December 3, 2023, 12:21am UTC](https://forum.ghost.org/t/redirect-www-to-non-www/20494/5 "2023-12-03T00:21:53Z")

</div>

Thought I’d just chime in here to give the direct route, with an explanation - while third-ing the automagical route.

If you’re on a minimal server (i.e. an E2-Micro on GCP) you’re going to have a hard time doing anything. Ideally, shutdown your Ghost instance, and MySQL, so that you have some guts driving your actions. Whichever direction you choose to take.

### Note

In both routes below, replace instances of domain.ext with your domain and domain extension. So if your domain was _[excuse.me](http://excuse.me)_, you’d replace _domain.ext_ with _**[excuse.me](http://excuse.me)**_

### Automagical Route

The above answers cleverly show you how to use the Ghost CLI (as the official guide directs) to (while adding in instruction to properly point the www host first):

1. Add an **A** record to your domain’s **dns entries** for your _ **www** _ host:

Then, after traversing to your Ghost instance directory (via `cd /var/www/<your_ghost_instance>`):

1. Configure Ghost temporarily for your **www** domain so that the Ghost CLI can work its magic:

2. Set up _ssl_ automatically with nginx for the **www** domain:

3. Configure your Ghost instance back to the _ **original** _ domain:

4. Manually edit the nginx configuration for your **www** domain so that it permanently redirects to the _ **non-www** _ variant:

5. Test nginx to ensure you did everything correctly:

6. Reload nginx if the test passed:

7. If needed (shouldn’t) restart your Ghost instance:

That should do it!

### Direct (Manual) Route

The direct method to do this is as follows:

1. Configure the **A** records for the _ **non-www** _, _ **www** _, and _ **remote server host name** _ for your Ghost instance server via your domain’s _ **dns entries** _:

Now on your server _(that you can now ssh into via_ `ssh host.domain.ext`_)_, traverse to your Ghost instance (`cd /var/www/<your_ghost_instance_directory`) and:

1. Configure a _ **www** _-variant host for nginx:

2. Configure a _ **www** _-variant _ **SSL** _ host for nginx:

3. Link both configurations to sites-enabled (this let’s you edit the file in sites-available, and as long as it’s linked in sites-enabled, it’s live):

Now we want to make some edits to the configurations so as to derive our www-variants from the non-www variants that already existed:

1. In `/etc/nginx/sites-available/www.domain.ext.conf`

2. In `/etc/nginx/sites-available/www.domain.ext-ssl.conf`:

3. Test that nginx configuration syntax is correct:

4. Restart nginx in order to reload the configuration:

5. Generate the SSH certificates for the www-variant domain:

6. If you had shut everything down to give your E2 Micro some guts, then:

That should do it!

### Conclusion

That’s how you can add an ssh certificate for every alternate domain and host you’d like to separately manage certificates for. I think it’s the ideal way to handle certificates in Ghost, even though you should be able to manage multiple hosts, or a wildcard, in a single domain (IIRC letsencrypt did not have any policy against that, but I could be outdated).

##### Renewal

With regards to renewal, there’s a straightforward automatic renewal configured by Ghost when you setup ssl on the primary domain, that will function for every certificate that is in the letsencrypt home (so any additional certificate you add, without having to do anything more than simply add the certificate).

To ensure you’re system is setup for that, run:

```bash
sudo crontab -l

```

You should see something akin to:

`18 9 * * * "/etc/letsencrypt"/acme.sh --cron --home "/etc/letsencrypt" > /dev/null`

If you don’t see the above as a result, you could attempt adding it manually:

```bash
crontab -e

```

Then use the example above to fill out the fields for your instance.

##### Manual Renewal

If the automatic renewal should ever fail (hey, it happens), there is no longer a way to manually renew it using the Ghost cli (from what I understand), but you could try using `acme.sh` in a couple of ways:

1. Attempt to manually run the cron (perhaps it was a temporary issue):

2. Attempt to leverage `acme.sh` directly to renew a specific domain:

##### Thanks

Albeit a bit selfishly, as this was the first result I found on the official forum for managing certificates, I’ve provided a thorough reply in hopes of covering future needs for myself - as well as for others.
