Redirect additional domain(s) to your Discourse instance

So you want to redirect domain(s) to your Discourse instance? Great, let’s get started!

In this tutorial we’ll show how to redirect www.example.com and example.com to talk.example.com.

Update DNS Record

Update the A record for the domain(s) you want to redirect to original domain.

Edit Configuration FIle

Open configuration file app.yml. From console, run these commands:

cd /var/discourse
git pull
nano containers/app.yml

Configuration file will open in nano editor. Search for hooks (with Ctrl+W) in the file:

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

Add after_web_config block to the hooks (proper indentation is important):

hooks:
  after_code:
    - exec:
        cd: $home/plugins
        cmd:
          - mkdir -p plugins
          - 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 80;
            server_name example.com;
            return 301 $scheme://talk.example.com$request_uri;
          }
    - file:
        path: /etc/nginx/conf.d/discourse_redirect_2.conf
        contents: |
          server {
            listen 80;
            server_name www.example.com;
            return 301 $scheme://talk.example.com$request_uri;
          }

You can add as many file blocks as you want, depending on number of domain(s) you want to redirect:

    - file:
        path: /etc/nginx/conf.d/discourse_redirect_3.conf
        contents: |
          server {
            listen 80;
            server_name discourse.example.com;
            return 301 $scheme://talk.example.com$request_uri;
          }

Modify the file name discourse_redirect_3 and domain name to which you want to redirect discourse.example.com as per your need.

Note that original domain name talk.example.com will remain same in all file blocks.

After completing your edits, press Ctrl+O then Enter to save and Ctrl+X to exit.

Rebuild Container

Run this command:

./launcher rebuild app

Congratulations, that’s it! Try visiting example.com and www.example.com, they both will redirect to talk.example.com!

38 Likes

Just a tip it is also a good idea to redirect commonly guessed urls to your main discourse install

e.g.  forum{s}.example.com => talk.example.com
10 Likes

What do I change

to? Where can I find the name?

1 Like

You can name the .conf file as you desire, but for easy understanding and consistent format, I named them as discourse_redirect_1.conf, discourse_redirect_2.conf, etc.

[NOTE: The number of .conf files required will depend on the number of domains you want to redirect. If you want to redirect a single domain (e.g www.domain.com to domain.com) then you will require a single .conf file, which you can also name as discourse_redirect.conf.]

It is not recommended to put Discourse on the root domain, because cookies set on the root domain carry over to all others, and you might end up with max-length cookie errors if you ever want anything else there.

4 Likes

Why would I want anything else? :smile:

I understand. That is exactly my problem. I can log out at non-www and stay logged in www.

CNAME www mysite.com.

I don’t remember this being explicit requirement during the discourse installation. Anyhow, it should work with the setup above.

I pretty much tried everything. Even consulted Digital Ocean guys. It seems if Discourse is installed in root there is no option to redirect www domain to non-www?

I followed the guide, and did rebuild.

I wanted to my www.example.com redirect to example.com

After the rebuild, I visited www.example.com

and I am seeing this.

What can I do now?

I tried with DigitalOcean Droplet, then redirection worked fine. (No further configuration required)

The problem above was not using DigitalOcean Droplet.

I am searching what further configuration is required… :frowning:

If anyone tell me anything… it will be much appreciated.

Hm… if example.com works fine, and the only problem you have is default page (Welcome to Nginx on Debian!) showing at www.example.com you just need to add this short server definition at the beginning of your vhost config file (probably something in /etc/nginx/sites-enabled/*):

server {
    listen 80;
    server_name www.example.com;
    return 301 http://example.com$request_uri
}

It will add permanent (301) HTTP redirect from www.example.com to example.com.
Remember to reload nginx afterwards.

1 Like

I edited /etc/nginx/sites-enabled/default

with:

server {
    listen 80;
    server_name www.example.com;
    return 301 http://example.com$request_uri;
}

And did:

root@ubuntu:/etc/nginx# sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@ubuntu:/etc/nginx# sudo service nginx reload
 * Reloading nginx configuration nginx                   [ OK ]

However I am still watching “Welcome to nginx” message :sweat_smile:

In that case I need to take a look at everything in /etc/nginx/sites-enabled/* to give you a meaningful answer.
If you are okay with it, PM me with configuration files.

You can set A record for www

Not the best solution because then you end up with duplicate sites that do not share cookies. That was my fix until I fixed nginx problem.

With your setup everything should work. I have added etc/nginx/sites-enabled/default because it was missing. I redirected opposite from non-www to www.

    listen 80;
    server_name example.com;
    return 301 http://www.example.com$request_uri;

With all this beauty still no redirection until I found error: server_names_hash_bucket_size: 32

So I went to nginx.conf and increased the default bucket size from 32 to 64. Now everything works.

2 Likes

I have been already using 2 A records for WWW and Non-WWW to same IP Address.

I had duplicate sites as you said.

But this “Welcome to Nginx” message problem started after I did this How-to Guide.

So I have lost my one dupilcate site as I said above, but the other one is working.

ps. I think I have no error in app.yml, but I will check one more time.

1 Like

Now… it is time to confess.

After 3 days of agony, I finally solved the problem.

I missed ;

I thought I was so sure about app.yml file… because I checked it again and again. (and didn’t realize that every time)

Anyway… I just found it.

Now everything is working well… :cry:

I am sorry to make you guys confused… this How-to guide is perfect.

9 Likes

Use http://www.yamllint.com/ to validate your yml files.

2 Likes

It was valid YAML. It was missing a ; in the command. We already validate YAML as part of the bootstrap.

Interestingly I remember missing ; in my translation files and yamlint helped.

Sorry, but after doing this should I point only my domain.com to droplet IP with an A record in order for this to work? Or should I make 3 A records (all pointing to the same IP) for talk.domain.com, domain.com and www.domain.com? Or perhaps 2 A records and 1 CNAME (for www to non-www redirect)?

All A records(www/talk/non-www) to the same IP. Then do above configuration in your app.yml. And this will let you redirect. That’s how I did! I didn’t do anything about Cname.

2 Likes