I'm trying to migrate a blog from mydomain.com to mydomain.com/blog but the images paths are broken, they point to /content/images instead of /blog/content/images even if I add subdir option in config.js -> paths. Also the usage of absolute="true" when posting the image does not change anything.
I've tracked the cause of this and here is where I'm at:
core/config/url.js/181
if (context === 'image' && data.image) {
// here absolute is true
urlPath = data.image;
imagePathRe = new RegExp('^' + ghostConfig.paths.subdir + '/' + ghostConfig.paths.imagesRelPath);
absolute = imagePathRe.test(data.image) ? absolute : false;
// but after the RegEx it will be false as in DB the path of the images does not contain
// subdir because the blog was previously on the root of the domain
secure = data.image.secure;
if (absolute) {
urlPath = urlPath.replace(new RegExp('^' + ghostConfig.paths.subdir), '');
baseUrl = getBaseUrl(secure).replace(/\/$/, '');
urlPath = baseUrl + urlPath;
}
return urlPath;
}
Also if I comment out the last line return urlPath; the script will go next to return createUrl(urlPath, absolute, secure); and this will return the correct urls for my images.
I'd appreciate any help on this. Thank you.
When you upload an image, Ghost writes a relative path for your image to the database image or markdown fields. You have changed what the correct relative path is, any links you created would also be broken in the same way. Ghost does not know that you have changed this and it does not doctor the text content of posts in any way.
Going through and fixing this is a necessary part of migrating between a subdirectory and non-subdirectory install. There may be some tools out there to help cc @cobbspur, but this is expected behaviour and not a bug.
@ErisDS For the markdown image URL it's expected to rewrite the URLs manually but what about the featured images/thumbnail? Those are broken as well.
Same deal, the relative URL is already written to the DB, changing the config isn't going to affect it. That's the problem with subdirectories, you're impacting the relative URL, not the hostname.
Essentially, Ghost doesn't have any built-in tools for migrating to/from having a subdir as this is absolutely an edgecase.
The URL is relative to the hostname as it starts with a slash. Without the slash it should work even if you're on a subdir but it won't work if you're on a inner-page (because the slug is considered as well).
The featured images can easily be solved by prefixing the path with the base URL - making them absolute but just for that render.
On the other hand, I wonder if the urls in the markdown can be fixed if you have this meta tag in the head
<base href="http://mydomain.com/blog/">
I had my nightmares migrating wordpress (as you all had) and I wish this won't be an issue in Ghost.
@deiucanta @erisds No I do not have a tool exclusively for this. There would be little point as in a case like this you only need to add the subdirectory section of the relative path so you would export your posts via labs. Open the file in a text editor and do a global replace of /content/images/ with /subdir/content/images/ and re-import
@cobbspur makes sense
@ErisDS any thoughts on using the meta tag for the base href?
@deiucanta That won't work because you'll get double subdirectories for all future uploads and any other items where the subdirectory is accounted for.
This is a one-time migration issue, you need to fix your bad data, as @cobbspur described, and then everything will work fine.
I just performed the the opposite: moving from subdir. on root domain ->subdomain. I only have ~10 or so pictures so this is not a huge deal. Seeing as the bug is approaching 3yrs old now & was created long before the Ghost-CLi tool, this could be a useful feature to implement into ghost config or maybe even doctor?
Personally I'm just manually fixing bad data as suggested, but going forward I would think supporting an easy migration from subdir->subdomain (or vice versa) wouldn't be too hard of an addition. I only mention this since LetsEncrypt offers Wildcard certs & the only reason I went the subdir. route in the first place was because of having a single cert. but wanting to force ssl connections only.
Just something to consider!
Most helpful comment
@deiucanta That won't work because you'll get double subdirectories for all future uploads and any other items where the subdirectory is accounted for.
This is a one-time migration issue, you need to fix your bad data, as @cobbspur described, and then everything will work fine.