Submitting headless site to Ghost Explore

Hey all,

I’ve been meaning to add my site to the somewhat new Ghost Explore directory as I quite like to be part of the larger community via it, but I’ve hit a snag with my setup. The initial flow for my site works really well, even though my site is using Ghost as a headless CMS is still finds the connected instance. However it does it a little too well, as the preview shows a link to my Ghost instance and not my live site.

I’ve tried to get around this by making a redirect that hooks into the ref=ghost-explore query parameter but it appears Ghost doesn’t let you redirect query parameters. Here’s what I did if you want to see the code from my redirects.json:

[
  { "from": "/?ref=ghost-explore", "to": "https://darn.es/" }
]

I’m really hoping that there’s some regex magic I can perform here and get this redirect to work. Any pointers?

Thanks :blush:

1 Like

Does the url in your Ghost config match your site’s url?

OK, so the problem is your Ghost site lives at https://hub.darn.es as a separate site to https://darn.es. Query param redirects aren’t supported in any way no matter the regex but I’m not sure adding a redirect will work in any case because the URL used in explore is the one that your Ghost site is configured to serve :thinking:

1 Like

Exactly! That’s a shame about query parameters. The Explore directory appears to add that to every site and I was hoping to leverage it as a chance to redirect. Does Ghost understand query parameters at all? I have for a moment considered using template code to check for it and add a meta tag with a redirect in it, ugly but not the worst :sweat_smile:

Update: In a moment of wishful thinking I tried using {{#has slug=""}} to check for a query parameter in the URL. Unsurprisingly it didn’t work but at least I know now

Slug wouldn’t ever pick up a query param, it will always match the post slugs.

In some hosting environments query params are stripped at the network edges so caching is consistent and potential DoS avenues are reduced so it’s not guaranteed that the Ghost instance itself will see query params.

However, query params will always be available in client code so you could do a JS redirect by putting a small script in the code injection head.

<script>
if ((new URLSearchParams(window.location.search)).get('ref') === 'ghost-explore') {
    window.location = 'https://your-redirect.com';
}
</script>
2 Likes

I thought that might be the case, just thought I’d give it a go to eliminate the option. Thanks for the snippet, JavaScript was my last resort for this issue. It’s not the end of the world if they land on my “hub” site, I just would’ve preferred something that doesn’t rely on client-side scripts. Will mark this as the answer :+1: