Redirect / mod_rewrite url

Hello,

I try to make a redirect in ghost.io(hosted version). I want to change the url from

"site.ghost.io/tour/?id=1200" to "site.ghost.io/tour/1200"

but it is not working my rule so I’m wondering it is possible with redirect to do this.

On a normal server you do this with .htaccess and mod_rewrite, this is an example.

 RewriteCond %{REQUEST_URI} !/$ [NC]
 RewriteCond %{REQUEST_URI} /tour.php?id=$ [NC]
 RewriteRule .* https://site.io/tour/$1 [R=301,L]

I try to do this with changing the redirects.json file and upload this file in labs. More about redirect you can read here: Implementing redirects in Ghost

Can you mod_rewrite url in ghost.io and is this possible with redirects or not?

Best,
Bram

If you’re hosted on Ghost(Pro), you can reach out to support via email.

Your examples are for Apache whereas Ghost usually sits behind an Nginx proxy. Nonetheless, for managed hosting, you won’t have this level of access.

Have you tried something like this redirects.json? I’ve not looked at this too closely, so there may be errors, but you should get the gist.

[{"from":"^/tour\/\?id=([0-9-]+)","to":"https://site.ghost.io/tour/$1","permanent":true}]
1 Like

Hi,

Thank you for you answer.
I create a redirect.yaml file based on your syntax:

301:
    ^/tour\/\?id=([0-9-]+): /tour/$1

and I try also with this:

301:
    ^/tour\/\?id=$: /tour/$1

but receive then a error 404.

Best,
Bram

I didn’t check my regex, as it was late and I was using my phone, but I did imply that you needed to check this.

Your example clearly won’t work since the regex doesn’t have back reference. Also, you must escape special characters.

PS. Please format code correctly… the forum editor has a button: </>

The ghost support did some testing on this, and including a redirect that contains a URL with a parameter in the “to” is not something that is supported yet. This is a limitation where ghost is aware of, however they do not have an ETA yet on when support for this would be implemented.

I note that you created a redirects.yaml; I use redirects.jsonwith URLs, but only because I have changed domains, and have two instances of Ghost running; one simply redirects to the other (I use regex, but back references.)

If Ghost uses the same domain as your previous site, then there’s no need for the domain in the redirect anyway.

The following is an example from the official documentation, so you should be able to achieve what you want.


301:
  ^\/blog\/([a-z0-9-]+)\/$: /$1

Yes this code is working fine, I use this for redirecting content after the id/* to one page

301:
  ^\/id\/([a-z0-9-]+)\/$: /$1

It is not possible to redirect tour/123 to tour/?id=123 inside the ghost website when tour/123 is no page. I load content from an API into tour/?id=123 , tour/?id=124, tour/?id=125,… but my goal was to create better URI’s. like tour/123, tour/124 or tour/125.

Sorry, I thought the objective was to redirect tour/?id=123 to tour/123.

You can only redirect successfully if a page / post exists. Indeed, I thought you were recreating pages in Ghost and wanted the old URL to point to the new location.