GHOST CMS - Redirects

Posted qduck

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GHOST CMS - Redirects相关的知识,希望对你有一定的参考价值。

Redirects

In addition to creating routes, you can also create redirects for any time there are any changes in your URLs and you need to forward visitors

Accessing the redirects file

The redirects.json file is located in content/data/redirects.json and - like routes.yaml - can also be downloaded/uploaded in the settings in Ghost Admin.

File structure

This file always starts with a [ and ends with a ]. A new ghost publication will have an empty redirect file containing []. It can be edited in any source code editor.

{
  "from": "/url/" ,
  "to":  "/url/",
  "permanent": true | false
}
  • The from field defines the incoming URL or pattern (regex)
  • The to field defines where the incoming traffic should be redirected to, which can be a static URL, or a dynamic value using regex (example: "to": "/$1/").
  • The permanent field can be defined with true for a permanent 301 redirect, or false for a temporary 302 redirect.

Multiple entries are separated by , and the last entry does not have the ,. Regular expressions can be used to implement redirect patterns.

Implementation

Upload your new redirects.json file in Ghost admin in the settings. This is the recommended method.

To replace the JSON file on the server, ensure it exists in content/data/redirects.jsonand run ghost restart for your changes to take effect.

When not to use redirects.json

There are some instances where it is not recommended to use the redirects.json file:

  • Page rules for www or HTTP/HTTPS redirection should always be implemented with your DNS provider.
  • Ghost automatically forces trailing slashes, so you do not need to write any page rules to accommodate for duplicate content caused by this.
  • If you‘re trying to change the URL structure of your publication, the recommended way to do this is with dynamic routing and the routes.yaml file. (However, you may still need to redirect existing content using redirects.json).

以上是关于GHOST CMS - Redirects的主要内容,如果未能解决你的问题,请参考以下文章

GHOST CMS - 配置 Config

GHOST CMS - Content Collections

GHOST CMS - Properties

GHOST CMS - Channels

GHOST CMS - Content Taxonomies

GHOST CMS - Data Helpers