# Broken image URL paths after changing hostname

**URL:** https://meta.discourse.org/t/broken-image-url-paths-after-changing-hostname/47722
**Category:** Support
**Created:** [7월 24, 2016, 5:43오후 UTC](https://meta.discourse.org/t/broken-image-url-paths-after-changing-hostname/47722 "2016-07-24T17:43:29Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![andy1247008998](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@andy1247008998](https://meta.discourse.org/u/andy1247008998)
#### Post date: [7월 24, 2016, 5:43오후 UTC](https://meta.discourse.org/t/broken-image-url-paths-after-changing-hostname/47722/1 "2016-07-24T17:43:29Z")

</div>

Here’s how to reproduce this bug:

1. ./launcher bootstrap app with `a.com` as hostname in app.yml
2. Upload a picture in a post. Let’s say it is `http://a.com/uploads/default/original/xxxx.png`
3. ./launcher rebuild app with `b.com` as hostname in app.yml
4. Open the post in Step 2, you will see a broken picture.
5. You will find that the picture is still refer to `http://a.com/uploads/default/original/xxxx.png` but not `b.com`
6. If you edit the post, the preview on the right side shows the picture correctly

This bug remains even if you destroy and bootstrap again.

Another way to produce this bug:

1. Back up `a.com`
2. Upload and restore to `b.com`
3. Pictures are broken

---

<div class="post-metadata">

### Author: ![Steven](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/steven/32/187890_2.png) [@Steven](https://meta.discourse.org/u/Steven)
#### Post date: [7월 24, 2016, 6:32오후 UTC](https://meta.discourse.org/t/broken-image-url-paths-after-changing-hostname/47722/2 "2016-07-24T18:32:22Z")

</div>

When you rebuild the post (rebuild html), is the problem fixed ?

If it’s fixed, you’ll have to rebale all posts

```
cd /var/discourse
./launcher enter app
rake posts:rebake

```

---

<div class="post-metadata">

### Author: ![andy1247008998](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@andy1247008998](https://meta.discourse.org/u/andy1247008998)
#### Post date: [7월 24, 2016, 6:45오후 UTC](https://meta.discourse.org/t/broken-image-url-paths-after-changing-hostname/47722/3 "2016-07-24T18:45:45Z")

</div>

Only rake posts:rebake won’t work.

But just one minute ago I found a “solution”:

```
discourse remap a.com b.com
rake posts:rebake

```

Then I got this:

```
...
Remapping uploads origin
1 rows affected!
Remapping oauth2_user_infos uid
0 rows affected!
Remapping oauth2_user_infos provider
0 rows affected!
Remapping oauth2_user_infos email
0 rows affected!
...

```

It seems that the “origin” url of the pics are hard coded into database, which are supposed to be stored as relative path.

Remap is not a solution because it may remap [thirdpartya.com](http://thirdpartya.com) to [thirdpartyb.com](http://thirdpartyb.com).

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [7월 24, 2016, 9:17오후 UTC](https://meta.discourse.org/t/broken-image-url-paths-after-changing-hostname/47722/4 "2016-07-24T21:17:04Z")

</div>

The URLs of the uploads are “_optimized_” (i.e. made absolute and use CDN when enabled) when cooking a post for performance reasons. The solution to your problem is indeed a remap. More can be found here

> [@Change the domain name or rename your Discourse](https://meta.discourse.org/t/change-the-domain-name-or-rename-my-discourse/16098):
>
> bookmark This guide explains how-to change the domain name of your Discourse site, and includes information about setting up DNS changes, editing configuration files, updating site settings, and correcting social logins and post content to reflect your new domain name. warning This guide is for self-hosted Discourse instances that were installed using our [installation guide on GitHub](https://github.com/discourse/discourse/blob/main/docs/INSTALL-cloud.md). If you are [hosted by Discourse](https://www.discourse.org/pricing), please see [Configure your domain name for hosted Discourse](https://meta.discourse.org/t/configure-your-domain-name-for-hosted-discourse/21827) for specific …

---

<div class="post-metadata">

### Author: ![andy1247008998](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@andy1247008998](https://meta.discourse.org/u/andy1247008998)
#### Post date: [7월 24, 2016, 11:02오후 UTC](https://meta.discourse.org/t/broken-image-url-paths-after-changing-hostname/47722/5 "2016-07-24T23:02:09Z")

</div>

Thank you for your response.

If the old hostname is not unique enough, then remap will generate unwanted results.

Besides, the URLs in editor are already stored as relative path. That’s why pics are working in preview mode.

For external pictures, they are “optimized”.  
For internal pictures, I would prefer {base\_url} + {relative\_path}.

cheers

---

<div class="post-metadata">

### Author: ![mpalmer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mpalmer/32/45740_2.png) [@mpalmer](https://meta.discourse.org/u/mpalmer)
#### Post date: [7월 24, 2016, 11:43오후 UTC](https://meta.discourse.org/t/broken-image-url-paths-after-changing-hostname/47722/6 "2016-07-24T23:43:24Z")

</div>

> [@andy1247008998](#):
>
> If the old hostname is not unique enough, then remap will generate unwanted results.

So don’t `remap example.com example.net`, but instead `remap //example.com/ //example.net/`.

---

<div class="post-metadata">

### Author: ![victoria](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@victoria](https://meta.discourse.org/u/victoria)
#### Post date: [7월 26, 2016, 8:59오전 UTC](https://meta.discourse.org/t/broken-image-url-paths-after-changing-hostname/47722/7 "2016-07-26T08:59:38Z")

</div>

Hello!

I’m having a similar problem with posted images on the forum I’m working on, I also have a [support thread](https://meta.discourse.org/t/avatar-images-and-posted-images/46549) about it.

The background is a little different: I haven’t renamed the forum, but I did migrate it to docker and set up HTTPS. We use AWS and the S3 bucket’s name had forbidden characters so we renamed the bucket, but all images are still pointing to the old bucket name which doesn’t work.

Is it possible to use remap to point these images to the new bucket name? Also, are you running remap from the same place as you run rake commands?

---

<div class="post-metadata">

### Author: ![mpalmer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mpalmer/32/45740_2.png) [@mpalmer](https://meta.discourse.org/u/mpalmer)
#### Post date: [7월 26, 2016, 9:04오전 UTC](https://meta.discourse.org/t/broken-image-url-paths-after-changing-hostname/47722/8 "2016-07-26T09:04:26Z")

</div>

> [@victoria](#):
>
> Is it possible to use remap to point these images to the new bucket name?

Yes. If the old bucket name is unique enough, you can just remap on that, otherwise you’ll want to expand out the string (to include, say, the full domain) to avoid false matches. The remap process is a very blunt instrument, it just does straight-up text search-and-replace.

> [@victoria](#):
>
> Also, are you running remap from the same place as you run rake commands?

Yes, the full command is `./script/discourse remap <oldstring> <newstring>`.

---

<div class="post-metadata">

### Author: ![victoria](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@victoria](https://meta.discourse.org/u/victoria)
#### Post date: [7월 26, 2016, 9:21오전 UTC](https://meta.discourse.org/t/broken-image-url-paths-after-changing-hostname/47722/9 "2016-07-26T09:21:27Z")

</div>

> [@mpalmer](#):
>
> If the old bucket name is unique enough, you can just remap on that

Well I don’t think that is so easy to know 😕

If the old bucket name is something like this  
`some.bucket.name.s3.amazonaws.com`, the new one is simply  
`some-bucket-name.s3.amazonaws.com`

If I understood correctly you mean that I could run the whole thing like so:

`discourse remap some.bucket.name.s3.amazonaws.com some-bucket-name.s3.amazonaws.com`

And that might be better?

> [@andy1247008998](#):
>
> If the old hostname is not unique enough, then remap will generate unwanted results.

Sorry if I’m asking very basic questions, but I really don’t want this to happen!

---

<div class="post-metadata">

### Author: ![mpalmer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mpalmer/32/45740_2.png) [@mpalmer](https://meta.discourse.org/u/mpalmer)
#### Post date: [7월 26, 2016, 9:24오전 UTC](https://meta.discourse.org/t/broken-image-url-paths-after-changing-hostname/47722/10 "2016-07-26T09:24:20Z")

</div>

> [@victoria](#):
>
> If I understood correctly you mean that I could run the whole thing like so:
> 
> discourse remap [some.bucket.name.s3.amazonaws.com](http://some.bucket.name.s3.amazonaws.com) [some-bucket-name.s3.amazonaws.com](http://some-bucket-name.s3.amazonaws.com)
> 
> And that might be better?

Yes, that’s the best way to do it, to avoid the possibility of someone having typed `some.bucket.name` into a message for some reason, and getting it rewritten accidentally.

---

<div class="post-metadata">

### Author: ![victoria](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@victoria](https://meta.discourse.org/u/victoria)
#### Post date: [7월 26, 2016, 9:57오전 UTC](https://meta.discourse.org/t/broken-image-url-paths-after-changing-hostname/47722/11 "2016-07-26T09:57:06Z")

</div>

Okay, so I ran that command, but I got this error:

```
/usr/local/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- thor (LoadError)
from /usr/local/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from ./script/discourse:3:in `<main>'

```

I’ll admit I’m not really great with ruby, not sure what is happening here. Any clues?

---

<div class="post-metadata">

### Author: ![mpalmer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mpalmer/32/45740_2.png) [@mpalmer](https://meta.discourse.org/u/mpalmer)
#### Post date: [7월 26, 2016, 10:10오전 UTC](https://meta.discourse.org/t/broken-image-url-paths-after-changing-hostname/47722/12 "2016-07-26T10:10:38Z")

</div>

Try running `bundle exec ./script/discourse remap <etc etc>`.

---

<div class="post-metadata">

### Author: ![victoria](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@victoria](https://meta.discourse.org/u/victoria)
#### Post date: [7월 26, 2016, 10:21오전 UTC](https://meta.discourse.org/t/broken-image-url-paths-after-changing-hostname/47722/13 "2016-07-26T10:21:52Z")

</div>

Okay, so this happened:

```
URGENT: FATAL: Peer authentication failed for user "discourse"
 Failed to initialize site default
Rewriting all occurences of mybucket
THIS TASK WILL REWRITE DATA, ARE YOU SURE (type YES)
YES
bundler: failed to load command: ./script/discourse (./script/discourse)
PG::ConnectionBad: FATAL: Peer authentication failed for user "discourse"

/var/www/discourse/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.7/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize'
...
/var/www/discourse/script/discourse:208:in `do_remap'

```

---

<div class="post-metadata">

### Author: ![victoria](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@victoria](https://meta.discourse.org/u/victoria)
#### Post date: [7월 26, 2016, 10:27오전 UTC](https://meta.discourse.org/t/broken-image-url-paths-after-changing-hostname/47722/14 "2016-07-26T10:27:57Z")

</div>

Got it to work now and pictures are back on the forum, here was how:

```
-udiscourse RAILS_ENV=production bundle exec ./script/discourse remap <old> <new>

```

Thanks for all the help! 😍

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [7월 26, 2016, 1:18오후 UTC](https://meta.discourse.org/t/broken-image-url-paths-after-changing-hostname/47722/16 "2016-07-26T13:18:33Z")

</div>

FYI: Once you’re inside the container (ie. after `./launcher enter app`) you should be able to run that command with only `discourse remap <old> <new>`.

---

<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: [2월 17, 2019, 2:51오전 UTC](https://meta.discourse.org/t/broken-image-url-paths-after-changing-hostname/47722/17 "2019-02-17T02:51:45Z")

</div>



---

<div class="post-metadata">

### Author: ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)
#### Post date: [2월 18, 2019, 5:26오전 UTC](https://meta.discourse.org/t/broken-image-url-paths-after-changing-hostname/47722/18 "2019-02-18T05:26:34Z")

</div>


