futureactivities/craft-custom-redirects

此包最新版本(1.0.3)没有可用的许可信息。

使用API支持定义自定义重定向

1.0.3 2024-09-04 10:57 UTC

This package is not auto-updated.

Last update: 2024-09-18 11:22:22 UTC


README

这为CMS提供了一个基本区域,用于定义自定义重定向。需要 Craft v4+

此插件 在前端不执行任何操作,它仅提供以下REST端点来检查是否存在重定向

/rest/v1/redirect

该端点期望一个body参数

uri = the full path, e.g. /mortgages

from字段支持正则表达式。示例

^/stansted?
^/(estate-agents/)?(\bedinburghwest\b)(/.*)?(/)?
^/estate-agents/(.*)/buying/?

示例用法

插件将以以下格式响应

{
    "from": "/one",
    "to": "/two",
    "code": "301"
}

然后您可以将其集成到所需的任何前端系统中。例如,对于Nuxt 3,这可能需要添加到通配符路由中,如下所示

const { data } = await useAsyncQuery(Url, { siteId: config.public.siteId, url: route.path.replace(/^\/+/g, '') });

// check if entry is null and trigger 404 error
if (!data.value || !data.value.entry) {
    // Check for a redirect
    const custom = await axios.post(config.public.cmsUrl + '/rest/v1/redirect', qs.stringify({ uri: route.path }));
    if (custom && custom.data)
        await navigateTo(custom.data.to, { redirectCode: custom.data.code })
    
    throw createError({ statusCode: 404, statusMessage: 'Page not found' });
}

导入

可以通过上传CSV到以下位置来导入重定向的CSV:/storage/redirect-import.csv

CSV应采用以下格式;

from, to, group, code, siteId

其中

group = A free text grouping of the redirects in the CMS
code = The status code, likely either 301 or 302

上传后,在CMS中导航到以下URL

/admin/customredirects/import

多站点支持

此插件支持多个站点。在调用端点时,将CMS基本URL设置为包含您的站点处理程序。例如

/de/rest/v1/redirect

待办事项

  • 创建CMS界面以上传CSV
  • 考虑GraphQL查询支持