Broken image URL paths after changing hostname

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

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 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 to thirdpartyb.com.

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

cheers

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

3 Likes

Hello!

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

2 Likes

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

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?

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 some.bucket.name 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
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'

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:

3 Likes

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

5 Likes