Broken image URL paths after changing hostname

Here’s how to reproduce this bug:

  1. ./launcher bootstrap app with as hostname in app.yml
  2. Upload a picture in a post. Let’s say it is
  3. ./launcher rebuild app with 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 but not
  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
  2. Upload and restore to
  3. Pictures are broken

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

Only rake posts:rebake won’t work.

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

discourse remap
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 to

1 Like

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

1 Like

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}.


So don’t remap, but instead remap // //



I’m having a similar problem with posted images on the forum I’m working on, I also have a support thread 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?

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.

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


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

If the old bucket name is something like this, the new one is simply

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

discourse remap

And that might be better?

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

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

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?

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

1 Like

Okay, so this happened:

URGENT: FATAL:  Peer authentication failed for user "discourse"
 Failed to initialize site default
Rewriting all occurences of mybucket
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'

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! :heart_eyes:


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