Thoughts, stories and ideas for web developers (with a focus on WordPress)

After changing your S3 CDN URL, the media URL of older posts won't get updated automatically. Luckily, it's pretty easy to do through a simple command.

When you're running Discourse, it's a really good idea to set up S3 instead of filling up your web server with all those media uploads. It's pretty easy to set up as well since everything is integrated already. You only need to enter some settings like your bucket name and access keys in the Discourse admin dashboard.

Setting up CDN URL

If you want to serve media uploads through your own subdomain instead of the default endpoint of AWS, Digital Ocean Spaces, Backblaze B2, etc, you just have to set up a subdomain. For this guide, I'll presume you set it up correctly at AWS, Digital Ocean Spaces, or whatever other S3 provider you're using.

When that's done, you go to your Discourse settings and filter for 'S3'. Scroll down to 's3 cdn url' and paste your subdomain.

Save the setting and try it out by uploading a new image in a post. The image should now be served from your subdomain. Neat!

Updating the CDN URL in old posts

However, media in old posts will still use the previous domain. To update the domain in old posts as well, Discourse suggests that you 'rebake' all posts, but they don't provide the command.

So SSH into your server and use the following commands:

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

Use 'remap' if you have a lot of posts

This will take a while, especially if you have a lot of posts. That's why it might be a better idea to use 'remap' instead of 'rebake' when you have a lot of posts.

If you want to use remap, use it like this:

rake posts:remap["find","replace"]
You’ve successfully subscribed to Teebow Blogs
Welcome back! You’ve successfully signed in.
Great! You’ve successfully signed up.
Success! Your email is updated.
Your link has expired
Success! Check your email for magic link to sign-in.