#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
| Name | Description |
|---|---|
_id | Assigned by MercuryCMS when the Redirect is created |
managedByAddonId | All of your created Redirects have this automatically added so they can be removed when the Addon is uninstalled |
createdAt | JavaScript Date the Redirect was created |
updatedAt | JavaScript Date the Redirect was last modified |
#Basic Properties
| Name | Description |
|---|---|
type | Either "plain" or "regex" |
description | A description of the Redirect's purpose. Because this field is uneditable by users, you can also use it to match against when upserting Redirects |
from | The path to redirect from |
to | The path to redirect to |
code | The HTTP status code to use. One of 301, 302, 307, 308, 410, 418, or 451 |
isEnabled | Set this to false to disable the Redirect, but the user can override this value |
notFoundOnly | Set this to true to only apply this Redirect if the path is not found |
body | The explanation shown when the code is 451 (Unavailable for Legal Reasons) |
#Query Properties
| Name | Description |
|---|---|
queryFrom | Either "ignore" or "include". "ignore" will match against just the path, while "include" will match against query parameters also |
queryTo | Either "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]})