Redirects

How to add redirects to your Ghost install

Use Nginx or Apache if you can

If you have access to your web server, and can add your redirects there, we'd always recommend doing this instead, as it's more performant and standardised.
If you don't have access to your webserver, Ghost's redirects are the next best thing 🙂

New redirects.json upload/download interface in Ghost 1.9.0+

As of Ghost 1.9.0 there is a new Labs feature where you can download and upload your redirects.json file within the admin interface. When using the admin interface there's no need to edit the file directly on your server or to restart Ghost after uploading.

Warning: Uploading a redirects.json file using the admin interface will overwrite any existing redirects.json file. Ensure you download the current redirects and edit the file before re-uploading to avoid accidental removal of existing redirect rules.

You can define your own HTTP redirects for your Ghost blog. The redirects file is a JSON file and needs to be located in content/data/redirects.json.

Each entry in the redirects file has the following structure:

{
  "from": "regex" ,
  "to":  "/url",
  "permanent": true | false
}

Permanent redirects (301) can be defined with permanent: true, otherwise we use 302 redirects as the default configuration.

Example:

[
    {
        "from": "^/post/[0-9]+/([a-z0-9\\-]+)",
        "to": "/$1"
    },
    {
        "permanent": true,
        "from": "/my-old-blog-post/",
        "to": "/revamped-url/"
    },
    {
        "from": "^\\/what(\\/?)$",
        "to": "/what-does-god-say"
    },
    {
        "from": "^\\/search\\/label\\/([^\\%20]+)$",
        "to": "/tag/$1"
    },
    {
        "from": "^\\/topic\\/",
        "to": "/"
    },
    {
        "from": "^/resources\\/download(\\/?)$",
        "to": "/shubal-stearns"
    },
    {
        "from": "^\\/[0-9]{4}\\/[0-9]{2}\\/([a-z0-9\\-]+)(\\.html)?(\\/)?$",
        "to": "/$1"
    },
    {
        "from": "^/prefix/([a-z0-9\\-]+)?",
        "to": "/blog/$1"
    }
]

Then restart ghost for the changes to take affect.


ghost restart

Common patterns

With or without trailing slash

code: (\\/?)$

For example redirecting "/recipes/" with or without the trailing slash, to /tag/recipes/.

 {
        "from": "^\\/recipes(\\/?)$",
        "to": "/recipes/"
},

Not sure about regular expressions (regex) ?

The best resource on the web to help with designing regular expressions is https://regex101.com/.

Once on the page, click "javascript" on the left hand side to use the most compatible mode. Then, in the top bar click the flag icon and also select "multiline".

Now, you can enter your pattern in the top bar, and then enter the paths you want to match, one-per-line, in the box below to check that your pattern works.

On the bottom right-hand-side, you'll find the "Quick Reference" with many examples of useful building blocks.

Redirects

How to add redirects to your Ghost install