【问题标题】:301 redirects with express使用 express 进行 301 重定向
【发布时间】:2018-03-09 20:00:31
【问题描述】:

我有一个 node.js 网站,它将替换现有网站。新站点中的某些 URL 与旧站点中的不同,因此我需要为这些设置 301 重定向。

目前我正在使用以下方法重新路由特定页面:

app.get('contact-us', (req, res, next) => { res.redirect(301, 'http://www.example.com/contact'); });

但是有没有办法根据模式重新路由?例如在旧网站上我有 /products/id/product-name 我需要重新路由到 /catalogue/id/product-name

【问题讨论】:

    标签: node.js express http-status-code-301


    【解决方案1】:

    您可以使用 express-redirect 模块有效地完成这项工作,

    您可以通过发出以下命令来安装它,

    npm install express-redirect
    

    要初始化,

    var express = require("express")
      , redirect = require("express-redirect");
    
    var app = express();
    redirect(app); 
    

    对于重定向,

    // just a simple redirect 
    app.redirect("/p/:id", "/page/:id");
    
    // you want it permanent? 
    app.redirect("/p/:id", "/page/:id", 301);
    
    // if you want to append the query string (?foo=bar) 
    app.redirect("/p/:id", "/page/:id", 301, true);
    

    希望这会有所帮助!

    【讨论】:

    • 我有一堆要重定向的页面,但没有子文件夹..例如app.redirect("/page1", "/pagepage1", 301); app.redirect("/page2", "/pagepage2", 301);也许成千上万......我该怎么做
    猜你喜欢
    • 2014-07-31
    • 2014-08-30
    • 2017-04-15
    • 2015-08-15
    • 2014-02-03
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多