【问题标题】:How do I properly setup a 301 redirect with my current nodejs express setup?如何使用当前的 nodejs express 设置正确设置 301 重定向?
【发布时间】:2023-03-29 06:29:01
【问题描述】:

我对如何设置重定向有点困惑。我的尝试不断导致“重定向过多”。

我希望我的 domain.herokuapp.com 重定向到 www.domain.com。

var express = require('express');
var router = express.Router();
var appdata = require('../data.json');

/* GET home page. */
router.get('/', function(req, res) {
   res.render('index', {
   title: 'Home',
   page: 'home'
  });
});

module.exports = router;

【问题讨论】:

    标签: node.js redirect express


    【解决方案1】:

    正如this 帖子所说,您可以使用res.redirect() 进行301 重定向:

    res.redirect(301, 'http://example.com');
    

    您可以查看有关重定向的快速文档:Express API Reference

    所以在你的情况下:

    router.get('/', function(req, res) {
        res.redirect(301, 'www.domain.com');
    });
    

    【讨论】:

    • 所以我尝试了这个,它给我的是这个结果:“domain.herokuapp.com/www.domain.com
    • 我猜你需要在你的网址中精确地http://。试试res.redirect(301, 'http://www.domain.com');
    猜你喜欢
    • 2012-07-07
    • 2020-01-21
    • 2011-01-22
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 2015-11-21
    • 2016-07-07
    相关资源
    最近更新 更多