# 永久链接中域名更改的URL重写

**URL:** https://meta.discourse.org/t/url-rewrite-for-domain-change-in-permalinks/58507
**Category:** Support
**Created:** [2017年三月6日 23:45 UTC](https://meta.discourse.org/t/url-rewrite-for-domain-change-in-permalinks/58507 "2017-03-06T23:45:37Z")
**Posts on this page:** 1
**Showing post:** 7

<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: [2017年三月8日 03:21 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 _;`.

---

_[View the full topic](https://meta.discourse.org/t/url-rewrite-for-domain-change-in-permalinks/58507)._
