# URL rewrite for domain change in permalinks

**URL:** https://meta.discourse.org/t/url-rewrite-for-domain-change-in-permalinks/58507
**Category:** Support
**Created:** [March 6, 2017, 11:45pm UTC](https://meta.discourse.org/t/url-rewrite-for-domain-change-in-permalinks/58507 "2017-03-06T23:45:37Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![modius](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/modius/32/108600_2.png) [@modius](https://meta.discourse.org/u/modius)
#### Post date: [March 6, 2017, 11:45pm UTC](https://meta.discourse.org/t/url-rewrite-for-domain-change-in-permalinks/58507/1 "2017-03-06T23:45:37Z")

</div>

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**

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

```

---

<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: [March 6, 2017, 11:47pm UTC](https://meta.discourse.org/t/url-rewrite-for-domain-change-in-permalinks/58507/2 "2017-03-06T23:47:31Z")

</div>

You should take care of this through nginx rewriting.

---

<div class="post-metadata">

### Author: ![modius](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/modius/32/108600_2.png) [@modius](https://meta.discourse.org/u/modius)
#### Post date: [March 6, 2017, 11:49pm UTC](https://meta.discourse.org/t/url-rewrite-for-domain-change-in-permalinks/58507/3 "2017-03-06T23:49:57Z")

</div>

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?

---

<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: [March 7, 2017, 12:27am UTC](https://meta.discourse.org/t/url-rewrite-for-domain-change-in-permalinks/58507/4 "2017-03-07T00:27:39Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![modius](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/modius/32/108600_2.png) [@modius](https://meta.discourse.org/u/modius)
#### Post date: [March 7, 2017, 2:08am UTC](https://meta.discourse.org/t/url-rewrite-for-domain-change-in-permalinks/58507/5 "2017-03-07T02:08:52Z")

</div>

> [@pfaffman](#):
>
> 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.

I see what you mean 🙂

- [discourse\_docker/templates at master · discourse/discourse\_docker · GitHub](https://github.com/discourse/discourse_docker/tree/master/templates)

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

---

<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: [March 7, 2017, 2:23am UTC](https://meta.discourse.org/t/url-rewrite-for-domain-change-in-permalinks/58507/6 "2017-03-07T02:23:37Z")

</div>

Have a look at let’s encrypt.

---

<div class="post-metadata">

### Author: ![modius](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/modius/32/108600_2.png) [@modius](https://meta.discourse.org/u/modius)
#### Post date: [March 8, 2017, 3:21am UTC](https://meta.discourse.org/t/url-rewrite-for-domain-change-in-permalinks/58507/7 "2017-03-08T03:21:06Z")

</div>

> [@modius](#):
>
> Would be sensational if one of those templates was an example of how to add a rewrite rule into the NGINX container.

Update your hostname for the forum:

```plaintext
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:

```plaintext
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 😉

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**

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:

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

```

More info on rebaking options here:

> [@Rebake all posts matching a pattern](https://meta.discourse.org/t/rebake-all-posts-matching-a-pattern/48713):
>
> bookmark This guide provides instructions on how to rebake all posts in Discourse that match a specific string or regular expression, utilizing the Rails console for additional advanced options. person_raising_hand Required user level: Administrator warning Console access required Want to rebake all the posts matching a string or regular expression? Let’s get started! Accessing your site To begin, connect to your Discourse Droplet via SSH and enter the Docker container for your Disc…

**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:

```plaintext
  ## 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 _;`.

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [June 8, 2024, 12:44pm UTC](https://meta.discourse.org/t/url-rewrite-for-domain-change-in-permalinks/58507/8 "2024-06-08T12:44:59Z")

</div>

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