@24hr/redirect-matcher 中文文档教程
WP redirect options
Wordpress plugin
Wordpress插件可以下载 这里。 它只会创建一个包含一些 ACF 字段的新选项页面并添加新的 API 端点。 它还会将选项注册到我们的 synk 插件。
Wordpress API 端点:/json/api/general/redirects
NPM module
有一个 NPM 模块,其中包含一个用于匹配 url 的辅助函数。 此助手支持通配符路径。
通过从 Wordpress 传递重定向选项以及请求路径,它将返回有用的数据。 如果不匹配,它将返回 null
。
返回的对象(如果匹配)包含要重定向到的URL、要设置为状态代码的代码和建议使用的标头。
Koa 示例:
const { redirectMatcher } = require('@24hr/redirect-matcher');
app.use(async (ctx, next) => {
const redirectOptions = await getRedirectOptionsSomehow(); // <-- This part is your responsibility
const redirectData = redirectMatcher(redirectOptions, ctx.path);
if (redirectData) {
ctx.status = redirectData.code;
ctx.set(redirectData.headers);
ctx.redirect(redirectData.url);
} else {
await next();
}
});
WP redirect options
Wordpress plugin
The Wordpress plugin can be downloaded here. It will just create a new options page with some ACF fields and add new API endpoint. It will also register the options to our synk plugin.
Wordpress API endpoint: /json/api/general/redirects
NPM module
There is a NPM module that includes a helper function to match urls. This helper supports wildcard paths.
By passing the redirect options from Wordpress along with the request path it will return useful data. If no match it will return null
.
Returned object (if match) contains the URL to redirect to, code to set as status code and headers that are recommended to use.
Koa example:
const { redirectMatcher } = require('@24hr/redirect-matcher');
app.use(async (ctx, next) => {
const redirectOptions = await getRedirectOptionsSomehow(); // <-- This part is your responsibility
const redirectData = redirectMatcher(redirectOptions, ctx.path);
if (redirectData) {
ctx.status = redirectData.code;
ctx.set(redirectData.headers);
ctx.redirect(redirectData.url);
} else {
await next();
}
});