How can I use dynamic redirects?

I’m migrating from vanilla to discourse, and we’ve decided to start fresh rather than import 8 years of topics. The current site is forums.29th.org, so I figure when I’ve got discourse fully up and running I’ll make discourse forums.29th.org and move vanilla to something else, like vanilla.29th.org (actually struggling to think of a good URL convention as we have several old forums at this point!)

So I’d like to redirect requests matching vanilla’s URL pattern to the other domain. This would be very easy with an nginx rule, but (a) I wanted to check whether it was possible to use a built-in feature of discourse, like Permalinks, and (b) if not, I wanted to ask how I’m meant to edit the nginx config the right way (it would be great if I could version control it too).

For conetxt, the vanilla topic URLs look like this:

/discussion/42206/example-topic#latest

Thanks!

I would use an nginx rule to redirect /discussion to the new/old domain. (And I would import the old data, but then, that’s how I earn my living)

@pfaffman Thanks - but how can I find out where to edit the nginx rule? Do I need to launcher enter app and modify the file at /etc/nginx/nginx.conf or something? Assuming they’re ephemeral containers, I’d have expected to edit something version controlled - is that possible?

You can add stuff to your app.yml to modify the nginx config (or run an external one). I’m not sure where best to send you. Here’s an example of making some changes to the nginx config for another purpose:

  after_ssl:
   # do not redirect all hosts back to the main domain name
    - replace:
        filename: "/etc/nginx/conf.d/discourse.conf"
        from: /if \(\$http_host[^\}]*\}/m
        to: ""
    - replace:
        filename: "/etc/nginx/conf.d/discourse.conf"
        from: /return 301.*$/
        to: "return 301 https://$host$request_uri;"

Perfect - that’s just what I needed to see. Thanks! Is this sort of thing documented somewhere that I could have looked at?

1 Like

For anyone else landing here, I implemented this by SSHing into my server, navigating to /var/discourse, opening containers/app.yml with vim and scrolling down to the hooks section. I then added this bit:

hooks: # this line should already be there
  after_ssl:
    - replace:
        filename: "/etc/nginx/conf.d/discourse.conf"
        from: "location @discourse {"
        to: |
          location /discussion/ {
            return 301 https://vanilla.29th.org$request_uri;
          }

          location @discourse {
  after_code: # this line should already be there
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.