URL rewrite for domain change in permalinks

I have a forum URL change from foo.discourse.com to bar.discourse.com.

If I have both domains pointing at the Discourse server is it possible to use Permalinks to redirect all foo domain traffic to the bar domain? Or do I have to take care of this elsewhere?

For Example

foo.discourse.com/blah/post -->  bar.discourse.com/blah/post

You should take care of this through nginx rewriting.

Am I right in thinking the standard Discourse Docker set up is fronted by an NGINX instance? And can that be modified without jeopardising the normal upgrade path?

I think what you want to do is to modify app.yml so that it adds to necessary lines to the nginx config inside the container to do the redirects. You might have a look at the yml files inside of samples for some examples, but I suspect you’ll need a bit more help.

1 Like

I see what you mean :slight_smile:

Would be sensational if one of those templates was an example of how to add a rewrite rule into the NGINX container.

Have a look at let’s encrypt.

Update your hostname for the forum:

DISCOURSE_HOSTNAME: 'www.new-name.com'

Add this to the run: section of your ./containers/app.yml for new domain rewrite url magic the NGINX way:

run:
  ## fix default discourse.conf
  - replace:
      filename: "/etc/nginx/conf.d/discourse.conf"
      from: "listen 80;"
      to: "listen 80 default_server;"
  ## Rewrite www.old-name.com to www.new-name.com
  - replace:
      filename: "/etc/nginx/conf.d/discourse.conf"
      from: "server {"
      to: |
        server {
            listen 80;
            listen 443 ssl;
            server_name www.old-name.com;
            return 301 $scheme://www.new-name.com$request_uri;
        }
        server {

PS. the YAML syntax is very particular about spacing, so make sure you have the indents just right :wink:

https://www.nginx.com/blog/creating-nginx-rewrite-rules/

Lastly, you may need to rebake your posts if they contain references to the old domain; these links will likely go to a dud tracking link as they now appear as external rather than “internal” links. This is fixed by:

cd /var/discourse
./launcher enter app
rake posts:rebake_match["www.old-name.com"]

More info on rebaking options here:

Separate note for Discourse maintainers…
Not sure how important this is overall @codinghorror but it looks like I can’t get a second server { } block to work properly without changing Discourse’s default discourse.conf to specify default_server like so:

  ## fix default discourse.conf
  - replace:
      filename: "/etc/nginx/conf.d/discourse.conf"
      from: "listen 80;"
      to: "listen 80 default_server;"

If you don’t nominate a default_server then the first server { } block is assumed to be the default and this causes problems.

It would be ideal if the default discourse.conf could be updated to include default_server. The other alternative is to use the DISCOURSE_HOSTNAME env to populate the server value instead of using server _;.

6 Likes

This topic was automatically closed after 2650 days. New replies are no longer allowed.