# How can I use dynamic redirects?

**URL:** https://meta.discourse.org/t/how-can-i-use-dynamic-redirects/172952
**Category:** Self-hosting
**Created:** [December 11, 2020, 7:06am UTC](https://meta.discourse.org/t/how-can-i-use-dynamic-redirects/172952 "2020-12-11T07:06:02Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![wilson29thid](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wilson29thid/32/173700_2.png) [@wilson29thid](https://meta.discourse.org/u/wilson29thid)
#### Post date: [December 11, 2020, 7:06am UTC](https://meta.discourse.org/t/how-can-i-use-dynamic-redirects/172952/1 "2020-12-11T07:06:02Z")

</div>

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:

```plaintext
/discussion/42206/example-topic#latest

```

Thanks!

---

<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: [December 11, 2020, 1:13pm UTC](https://meta.discourse.org/t/how-can-i-use-dynamic-redirects/172952/2 "2020-12-11T13:13:18Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![wilson29thid](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wilson29thid/32/173700_2.png) [@wilson29thid](https://meta.discourse.org/u/wilson29thid)
#### Post date: [December 11, 2020, 6:20pm UTC](https://meta.discourse.org/t/how-can-i-use-dynamic-redirects/172952/3 "2020-12-11T18:20:38Z")

</div>

@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?

---

<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: [December 11, 2020, 7:39pm UTC](https://meta.discourse.org/t/how-can-i-use-dynamic-redirects/172952/4 "2020-12-11T19:39:30Z")

</div>

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:

```plaintext
  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;"

```

---

<div class="post-metadata">

### Author: ![wilson29thid](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wilson29thid/32/173700_2.png) [@wilson29thid](https://meta.discourse.org/u/wilson29thid)
#### Post date: [December 12, 2020, 7:24am UTC](https://meta.discourse.org/t/how-can-i-use-dynamic-redirects/172952/5 "2020-12-12T07:24:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![wilson29thid](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wilson29thid/32/173700_2.png) [@wilson29thid](https://meta.discourse.org/u/wilson29thid)
#### Post date: [December 12, 2020, 11:53am UTC](https://meta.discourse.org/t/how-can-i-use-dynamic-redirects/172952/6 "2020-12-12T11:53:54Z")

</div>

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:

```yaml
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

```

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [January 11, 2021, 11:53am UTC](https://meta.discourse.org/t/how-can-i-use-dynamic-redirects/172952/7 "2021-01-11T11:53:55Z")

</div>

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