# Setup Let’s Encrypt + non-www \> www

**URL:** https://meta.discourse.org/t/setup-let-s-encrypt-non-www-www/84840
**Category:** Sysadmins
**Tags:** how-to, domains
**Created:** [April 8, 2018, 10:33pm UTC](https://meta.discourse.org/t/setup-let-s-encrypt-non-www-www/84840 "2018-04-08T22:33:44Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![BlackKnob](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blackknob/32/93549_2.png) [@BlackKnob](https://meta.discourse.org/u/BlackKnob)
#### Post date: [April 8, 2018, 10:33pm UTC](https://meta.discourse.org/t/setup-let-s-encrypt-non-www-www/84840/1 "2018-04-08T22:33:44Z")

</div>

I finally made it work, and thought to put everything together in case someone needs it.  
This info is assembled from different topics on this forum. Thanks to @brahn and @techAPJ.  
This assumes you’ve completed the initial Let’s Encrypt setup:

> [@Set up HTTPS support with Let's Encrypt](https://meta.discourse.org/t/setting-up-lets-encrypt/40709):
>
> bookmark This is a guide for enabling HTTPS on an existing Discourse installation using Let’s Encrypt. It assumes prior installation without HTTPS enabled. person_raising_hand Required user level: System Administrator exclamation This guide is only for existing installs where HTTPS is not enabled. Following the [official setup guide](https://github.com/discourse/discourse/blob/main/docs/INSTALL-cloud.md) automatically enables HTTPS by default. So you’d like to add https to your Discourse absolutely free, courtesy of our friends at [Let’s Encrypt?](https://letsencrypt.org/)bell I…

After you’ve done it, the following needs to be added to the app.yml in hooks section. It will do two things:

1. setup SSL for both `example.com` and `www.example.com`. Just replace `example.com` with your domain name.
2. redirect `example.com` to `www.example.com`

If anybody knows how to improve it, please let me know.

```
hooks:
  after_code:
    - exec:
        cd: $home/plugins
        cmd:
          - git clone https://github.com/discourse/docker_manager.git

  after_web_config:
    - replace:
        filename: /etc/nginx/nginx.conf
        from: /sendfile.+on;/
        to: |
          server_names_hash_bucket_size 64;
          sendfile on;
    - file:
        path: /etc/nginx/conf.d/discourse_redirect_1.conf
        contents: |
          server {
            listen 443;
            server_name example.com;
            return 301 $scheme://www.example.com$request_uri;
          }
  after_ssl:
    - replace:
        filename: "/etc/runit/1.d/letsencrypt"
        from: /-k 4096 -w \/var\/www\/discourse\/public/
        to: |
          -d www.example.com -d example.com -k 4096 -w /var/www/discourse/public

    - replace:
        filename: "/etc/runit/1.d/letsencrypt"
        from: /-k 4096 --force -w \/var\/www\/discourse\/public/
        to: |
          -d www.example.com -d example.com -k 4096 --force -w /var/www/discourse/public

    - replace:
        filename: "/etc/nginx/conf.d/discourse.conf"
        from: /return 301 https.+/
        to: |
          return 301 https://$host$request_uri;

    - replace:
        filename: "/etc/nginx/conf.d/discourse.conf"
        from: /gzip on;[^\}]+\}/m
        to: |
          gzip on;
          add_header Strict-Transport-Security 'max-age=31536000'; # remember the certificate for a year and automatically connect to HTTPS for th$

```

as always, remember that spacing and indentation is extremely important in the app.yml file. After you’ve done editing, you have to rebuild the container with:

```
cd /var/discourse
./launcher rebuild app

```

---

<div class="post-metadata">

### Author: ![BlackKnob](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blackknob/32/93549_2.png) [@BlackKnob](https://meta.discourse.org/u/BlackKnob)
#### Post date: [April 8, 2018, 11:47pm UTC](https://meta.discourse.org/t/setup-let-s-encrypt-non-www-www/84840/2 "2018-04-08T23:47:18Z")

</div>

I wonder if this part is needed, since we already have the “force https” option in Admin settings.  
@brahn, this is your part of the code, can you explain why is this necessary?

> ```
> - replace:
> filename: "/etc/nginx/conf.d/discourse.conf"
> from: /return 301 https.+/
> to: |
> return 301 https://$host$request_uri;
> 
> ```

---

<div class="post-metadata">

### Author: ![brahn](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/brahn/32/109267_2.png) [@brahn](https://meta.discourse.org/u/brahn)
#### Post date: [April 9, 2018, 3:27am UTC](https://meta.discourse.org/t/setup-let-s-encrypt-non-www-www/84840/3 "2018-04-09T03:27:42Z")

</div>

> [@BlackKnob](#):
>
> force https

I’m not really sure, does that setting redirect or just block non-https requests?

---

<div class="post-metadata">

### Author: ![Dmitry\_Krasnoperov](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dmitry_krasnoperov/32/65079_2.png) [@Dmitry\_Krasnoperov](https://meta.discourse.org/u/Dmitry_Krasnoperov)
#### Post date: [June 20, 2021, 12:48pm UTC](https://meta.discourse.org/t/setup-let-s-encrypt-non-www-www/84840/7 "2021-06-20T12:48:56Z")

</div>

I need to put full context.  
I have pretty fresh version 2.7.0.beta7 but my www and non-www config is incorrect. I try to fix it

I try instruction above (copy/paste) and now my non-www stops working.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [June 20, 2021, 4:06pm UTC](https://meta.discourse.org/t/setup-let-s-encrypt-non-www-www/84840/8 "2021-06-20T16:06:46Z")

</div>

That path is inside the container, so if you’re looking on the host os then you’re looking in the wrong place.

You might check [Setting up Let’s Encrypt with Multiple Domains](https://meta.discourse.org/t/setting-up-let-s-encrypt-with-multiple-domains/56685) and see if those directions are different. You’d just add the other domain the same way.

You can also check out [https://www.forcewww.com/](https://www.forcewww.com/)

---

<div class="post-metadata">

### Author: ![tomtjes](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tomtjes/32/131271_2.png) [@tomtjes](https://meta.discourse.org/u/tomtjes)
#### Post date: [February 23, 2025, 1:04am UTC](https://meta.discourse.org/t/setup-let-s-encrypt-non-www-www/84840/9 "2025-02-23T01:04:09Z")

</div>

This seems to no longer work and cause my instance to be unreachable. Got it back in working condition using this instead:

> [@Set up Let’s Encrypt with multiple domains / redirects](https://meta.discourse.org/t/set-up-let-s-encrypt-with-multiple-domains-redirects/56685):
>
> NOTE: @pfaffman says: This page needs to be cleaned up. There is now a new ENV setting that will let you add more hostnames. In your app.yml under your DISCOURSE\_HOSTNAME line (it can go many places but that one makes sense), add DISCOURSE\_HOSTNAME\_ALIASES: domain.com,other.domain.com and early reports suggest that you’ll get valid certs for those domains and that accessing https://domain.com will properly redirect you to your DISCOURSE\_HOSTNAME without a certificate error. If you do that an…
