Skip to content

40%off everything for Black Friday, with code . Until Dec 1.

Bright Themes

How redirects work in Ghost

redirects.yaml sends old URLs to new ones, so links and search rankings survive a renamed post, a migration or new permalinks. How it works, with plain and regex examples you can copy.

Norbert4 min read

Ghost keeps its redirects in one file, redirects.yaml. Each rule sends an old path to a new one, or to another website. Readers, newsletters and search engines hold links to exact URLs, so when you rename a post, move from another platform or change your permalinks, redirects keep those links working.

Redirects don’t decide your URL structure: that’s routing, in routes.yaml. They bridge old URLs to the new ones after you change it. Ghost’s implementing redirects (opens in a new tab) tutorial is the full reference.

301 or 302

CodeWhat it saysUse it for
301Moved for good. Search engines move the old URL’s ranking to the new one.Renamed slugs, migrations from WordPress, Medium or Substack, merged duplicate URLs, moving /articles/ to /blog/
302Moved for now. The old URL is expected back.Short maintenance moves, campaigns, anything you plan to revert

When in doubt, and after every migration, use 301.

Where to edit redirects.yaml

In Ghost Admin, go to Settings › Advanced › Labs › Beta features. The Redirects row has an Edit button to change the file in the browser, and a menu to Download file and Upload file if you’d rather edit it in a code editor.

Settings › Advanced › Labs › Beta features

Ghost checks the whole file when you save or upload it. If one rule is invalid, such as a broken regular expression, it rejects the file and your current redirects stay as they are. A new site starts with no redirects.

Older sites may still have their redirects as JSON; Ghost reads that too, but write new rules in YAML.

The file format

The file has two keys, 301 and 302. Under each, list source: destination pairs. For pages on your own site, write only the path, without the domain:

YAML
301:
  /old-post-slug/: /new-post-slug/
  /another-from/: /another-to/

302:
  /temporary-source/: /temporary-destination/

Indent with spaces, never tabs. Ghost URLs end with a slash, so write your rules the same way, and test both with and without the slash if a rule doesn’t fire.

To remove every redirect, save the file with both keys and no rules under them:

YAML
301:

302:

Simple redirects

One post renamed:

YAML
301:
  /old-slug/: /new-slug/

A tag renamed, or tag URLs changed in routes.yaml:

YAML
301:
  /tag/old-label/: /tag/new-label/

To another website, with the full URL:

YAML
301:
  /legacy-page/: https://example.com/docs/new-location/

Several old URLs to one:

YAML
301:
  /editing-a-post/: /editing/
  /editing-posts/: /editing/

Regex redirects

A plain path matches one URL. A regular expression matches many at once, which is what you need after a migration or a permalink change. Ghost uses JavaScript regular expressions. In the destination, $1, $2 and so on insert what the groups in brackets captured.

Warning

A wrong pattern can cause redirect loops, mass 404s or redirects you didn’t mean. Test each pattern on regex101 (opens in a new tab) with the ECMAScript (JavaScript) flavor, add one rule at a time, and try it on a staging site first if you have one.

Remove a path prefix

Your old site had posts at /blog/post-slug/; Ghost has them at /post-slug/:

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

The group captures the slug after /blog/, and the visitor goes to /{slug}/. Change [a-z0-9-]+ if your slugs have other characters.

WordPress dated URLs

Year, month and day in the path (/2020/03/15/my-post/ → /my-post/):

YAML
301:
  ^\/[0-9]{4}\/[0-9]{2}\/[0-9]{2}\/(.*): /$1

Year and month only (/2020/03/my-post/ → /my-post/):

YAML
301:
  ^\/[0-9]{4}\/[0-9]{2}\/(.*): /$1

These work only when the Ghost slug is the same as the old one. Where the importer changed slugs, add a plain rule for each of those posts.

Categories to tags

Imports usually turn categories into tags:

YAML
301:
  ^\/category\/(.*): /tag/$1

A whole section

Redirects match URL paths, not collection names. If your posts moved from /articles/ to /blog/ with the same slugs:

YAML
301:
  ^\/articles\/(.*)$: /blog/$1

Or from /articles/my-post/ to the root, /my-post/:

YAML
301:
  ^\/articles\/(.*)$: /$1

Put specific rules above broad patterns, and use plain rules before you reach for one large regex.

From other platforms

Squarespace’s /blog/ prefix to a root slug:

YAML
301:
  ^\/blog\/(\S+): /$1

Medium adds an ID to the end of each slug. This strips it; check it against your real URLs first:

YAML
301:
  ^\/(?!p\/)(.*)(-[0-9a-f]{10,12}): /$1

Substack uses /p/ for posts, and Ghost uses /p/ for post previews, so these need care: start from the Substack example in Ghost’s implementing redirects (opens in a new tab) tutorial.

When not to use redirects

  • HTTPS and www: set these at your DNS, host or reverse proxy (such as Caddy or nginx), not in redirects.yaml.
  • Trailing slashes: Ghost already adds them; you don’t need rules for that.
  • Designing your URLs: use routes.yaml for /blog/, collections and taxonomies, and redirects only for the old URLs left behind.
  • A new domain: that’s DNS and your Ghost config; see how to change your domain in self-hosted Ghost.

Redirects after a routing change

A change to collections or permalinks in routes.yaml changes post URLs at once, and every existing link still points to the old ones. After you change your routing:

  1. List the old URLs: from your old platform’s export, your analytics or Google Search Console.
  2. Add 301 rules from the old paths to the new ones.
  3. Save the file and test the old URLs.

For example, if you moved the post list from / to /blog/ and your posts’ permalink became /blog/{slug}/, the old /my-post/ needs a rule to /blog/my-post/. A custom homepage is the usual reason for that change.

Test your redirects

  • Open an old URL in a private window, so your browser cache doesn’t hide the result.
  • Check the response with curl -I https://yoursite.com/old-path/: you should see 301 (or 302) and the new URL in the location header.
  • Spot-check the URLs that get the most visits or links.

If a rule doesn’t work:

  • Slash mismatch: the source or destination lacks the trailing slash your URLs use.
  • Pattern too broad: (.*) matches more than you meant. Narrow it, and anchor it with ^ and $.
  • Two rules for one path: remove one and test again.
  • CDN cache: an edge cache can serve the old response for a while after you save.

Changing where posts appear rather than where they redirect? That’s a collection filter: see filter expressions in Ghost.

Related posts

More on routing

Get the Bright Themes newsletter

New tutorials and explainers, what changed in Ghost, and our theme releases. No spam, unsubscribe anytime.

Get the Bright Themes newsletter

New tutorials and explainers, what changed in Ghost, and our theme releases. No spam, unsubscribe anytime.