首页 > 解决方案 > 如何在 Gatsby 中实现到外部站点的重定向?

问题描述

如何在 Gatsby 中实现从/foobar/到外部 url的重定向http://example.com/page/

对于 Gatsby 2.20.6,此代码不起作用:

// gatsby-node.js

...

exports.createPages = ({ actions }) => {
    const { createRedirect } = actions;

    createRedirect({
        fromPath: '/foobar/',
        isPermanent: true,
        redirectInBrowser: true,
        toPath: 'http://example.com'
    });
});

标签: redirectgatsby

解决方案


createRedirect使其他插件有机会在您的静态站点构建期间生成重定向。根据您实际托管该静态站点的位置,进行重定向的过程可能会完全不同。

有关 GatsbycreateRedirect文档的更多背景信息:

服务器重定向不能开箱即用。您必须有一个插件设置才能将重定向数据与您的托管技术集成

希望有帮助!


推荐阅读