/ Engineering

Ghost blog images not working after move to subdirectory?

As search engines mostly regard subdomains as a separate domain in ranking algorithms, we moved our Ghost blog from a subdomain to a subdirectory to optimise our search engine performance.

Ghost v0.4 has support for subdirectories out of the box, but there is some configuration that needs to be done. There is a good post on how to do this here.

If you moved your blog with existing content and images, you'll realise that your image links are broken after moving the blog. This is because image links in posts are hardcoded.

Instead of manually reuploading all your images, here is a simple workaround for those of you out there using a sqlite3 database, which is what Ghost uses out of the box. It involves running a search and replace on the database, so USE AT YOUR OWN RISK.

Step 1: Make backup of database

# in Ghost directory in terminal
cp content/data/ghost.db content/data/ghost.backup.db

Step 2: Replace hardcoded image links with new ones

sqlite3 content/data/ghost.db

# assuming your subdirectory is /blog 
UPDATE posts SET markdown = replace(markdown, '![](/content/', '![](/blog/content/');
UPDATE posts SET html = replace(html, '<img src="/content/', '<img src="/blog/content/');

# quit sqlite3

Now restart your blog and you should be set!

Justin Yek

Justin Yek

Partner & Cofounder @ Altitude Labs. Responsible for our in-house personalization technology Metisa.

Read More