Basics
Features
Best Practices
Addon Development
Related Docs
Dark Mode

#Redirects

Your Addon can create and manage its own Redirects. These cannot be edited by the user, other than to disable or enable them.

#Redirect Schema

{
  // uneditable properties
  _id: '66483417384a5c3458f40277',
  managedByAddonId: '639b8f837f5fb8a39404280a',
  createdAt: 1719981015946,
  updatedAt: 1719982238694,

  // basic properties
  type: 'plain',
  description: '',
  from: '',
  to: '',
  code: 301,
  isEnabled: true,
  notFoundOnly: false,
  body: '',

  // query parameters
  queryFrom: 'ignore',
  queryTo: 'keep',

}

#Uneditable Properties

NameDescription
_idAssigned by MercuryCMS when the Redirect is created
managedByAddonIdAll of your created Redirects have this automatically added so they can be removed when the Addon is uninstalled
createdAtJavaScript Date the Redirect was created
updatedAtJavaScript Date the Redirect was last modified

#Basic Properties

NameDescription
typeEither "plain" or "regex"
descriptionA description of the Redirect's purpose. Because this field is uneditable by users, you can also use it to match against when upserting Redirects
fromThe path to redirect from
toThe path to redirect to
codeThe HTTP status code to use. One of 301, 302, 307, 308, 410, 418, or 451
isEnabledSet this to false to disable the Redirect, but the user can override this value
notFoundOnlySet this to true to only apply this Redirect if the path is not found
bodyThe explanation shown when the code is 451 (Unavailable for Legal Reasons)

#Query Properties

NameDescription
queryFromEither "ignore" or "include". "ignore" will match against just the path, while "include" will match against query parameters also
queryToEither "keep" or "replace". "keep" will retain the query parameters and append/merge them with the to path, while "replace" will replace the path with exactly what's in the to field

#Creating Redirects

You can create Redirects using cms.upsertRedirect().

let redirectDef = {
  type: 'regex',
  description: 'Redirects not found blog pages to the main blog page. Ex: /blog/page/10 to /blog if there are only 9 pages.',
  from: '^/blog/page/.*',
  to: '/blog',
  code: 307,
}

// use static-cms-addon
import cms from 'static-cms-addon'

let redirect = await cms.upsertRedirect({from: '^/blog/page/.*'}, redirectDef)
console.log(redirect._id) // "66483417384a5c3458f40277"

// use axios
let response = await axios.post(`http://localhost:${cmsPort}/api/addon/${addonId}/redirect/upsert`, {match: {from: '^/blog/page/.*'}, redirect: redirectDef})
console.log(response.data.redirect._id) // "66483417384a5c3458f40277"

#Listing Redirects

You can use cms.getRedirects() to get a list of the Redirects your Addon has created.

// use static-cms-addon
import cms from 'static-cms-addon'

let redirects = await cms.getRedirects()
console.log(redirects.map(redirect => redirect.from)) // ['^/blog/page/.*']

// use axios
let response = await axios.get(`http://localhost:${cmsPort}/api/addon/${addonId}/redirect`)
console.log(response.data.redirects.map(redirect => redirect.from)) // ['^/blog/page/.*']

#Deleting Redirects

You can delete Redirects your Addon has created using cms.deleteRedirects().

// use static-cms-addon
import cms from 'static-cms-addon'

let redirects = await cms.getRedirects()
let redirect = redirects.find(redirect => redirect.from == '^/blog/page/.*')
await cms.deleteRedirects([redirect._id])

// use axios
let response = await axios.get(`http://localhost:${cmsPort}/api/addon/${addonId}/redirect`)
let redirect = response.data.redirects.find(redirect => redirect.from == '^/blog/page/.*')
await axios.post(`http://localhost:${cmsPort}/api/addon/${addonId}/redirect/delete`, {redirectIds: [redirect._id]})