# Correct way to add rewrite rules to Nginx

**URL:** https://meta.discourse.org/t/correct-way-to-add-rewrite-rules-to-nginx/24679
**Category:** Self-hosting
**Created:** [2월 1, 2015, 2:15오후 UTC](https://meta.discourse.org/t/correct-way-to-add-rewrite-rules-to-nginx/24679 "2015-02-01T14:15:59Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![peter\_backx](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/peter_backx/32/115416_2.png) [@peter\_backx](https://meta.discourse.org/u/peter_backx)
#### Post date: [2월 1, 2015, 2:15오후 UTC](https://meta.discourse.org/t/correct-way-to-add-rewrite-rules-to-nginx/24679/1 "2015-02-01T14:15:59Z")

</div>

I’m currently migrating a discussion board to Discourse. I’m still learning about how Discourse is distributed and set up, but I’ve got the data imported and everything configured.

After importing all the topics, I now have a long list of mappings from the old topic id to the new Discourse topic id.

I’d like to put all of these into rewrite rules and add them to the Nginx server.

My question: what is the best way to do this?

I don’t think it’s a good idea to just put them in the /etc/nginx/conf.d/discourse.conf as that may cause issues when upgrading Discourse in the future. Or won’t it?

Can I put them in a separate conf? Will that file survive when the server restarts and/or updates are made to the Docker container? (or is that not even an issue and should I stop worrying 🍹 )

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2월 1, 2015, 8:36오후 UTC](https://meta.discourse.org/t/correct-way-to-add-rewrite-rules-to-nginx/24679/2 "2015-02-01T20:36:01Z")

</div>

Add a template or a hook and do a replace rule. see the ssl template in the templates directory for an example.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [2월 1, 2015, 8:42오후 UTC](https://meta.discourse.org/t/correct-way-to-add-rewrite-rules-to-nginx/24679/3 "2015-02-01T20:42:45Z")

</div>

No I think he means a list of old ID mapping to new IDs. @neil didn’t you add support for this kind of lookup table somewhere?

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2월 1, 2015, 8:58오후 UTC](https://meta.discourse.org/t/correct-way-to-add-rewrite-rules-to-nginx/24679/4 "2015-02-01T20:58:04Z")

</div>

Yeah, for that there is this howto

> [@Redirect old forum URLs to new Discourse URLs using permalinks](https://meta.discourse.org/t/redirecting-old-forum-urls-to-new-discourse-urls/20930):
>
> Redirecting Old Forum URLs to New Discourse URLs using permalinks If you’ve moved from other forum software to Discourse using [one of our import scripts](https://github.com/discourse/discourse/tree/main/script/import_scripts), then you probably want all your hard-earned Google search results to continue pointing to the same content. Discourse has a built-in way to handle this for you as an alternative to writing nginx rules, using the permalinks lookup table. The permalinks table allows you to set two things: a url to match, and what that url should show. There a…

---

<div class="post-metadata">

### Author: ![peter\_backx](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/peter_backx/32/115416_2.png) [@peter\_backx](https://meta.discourse.org/u/peter_backx)
#### Post date: [2월 2, 2015, 8:11오전 UTC](https://meta.discourse.org/t/correct-way-to-add-rewrite-rules-to-nginx/24679/5 "2015-02-02T08:11:57Z")

</div>

Thanks for the pointer.

Since I’m a Ruby novice and I barely know enough to be dangerous, I’d prefer to keep this separate. It will also be easier (I hope) to update on the fly, if it turns out I forgot to map some rules once I have flipped the switch and turned on the Discourse forum.

I’m going to try to use the Nginx map module first and see how that goes:

[https://meta.discourse.org/t/redirecting-old-forum-urls-to-new-discourse-urls/20930/10?u=peter\_backx](https://meta.discourse.org/t/redirecting-old-forum-urls-to-new-discourse-urls/20930/10)

---

<div class="post-metadata">

### Author: ![neil](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neil/32/102150_2.png) [@neil](https://meta.discourse.org/u/neil)
#### Post date: [2월 3, 2015, 4:04오후 UTC](https://meta.discourse.org/t/correct-way-to-add-rewrite-rules-to-nginx/24679/6 "2015-02-03T16:04:55Z")

</div>

> [@peter\_backx](#):
>
> I’d prefer to keep this separate. It will also be easier (I hope) to update on the fly

It’s definitely a preference. Yesterday I was able to add one rule to one site’s permalinks table because they wanted a certain redirect from their old forums to their new forums that we host. In our environment, this was much easier than updating haproxy config. I don’t want to fill our config files with countless arbitrary redirects from all our customers. It would be insane for us, but maybe not for you.
