如何在 Node.js/Express 中进行重定向 (301)? [英] How to make a redirect (301) in Node.js / Express?
本文介绍了如何在 Node.js/Express 中进行重定向 (301)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个静态网站,我在 Dokku 中部署了一个简单的单页面布局.我需要从 www 到非 www 以及从 *.website1.com
到 website2.com
进行重定向 (301),但我不知道如何.我试图在 npm 中使用 express-force-domain 但它使我的 URL 变得疯狂并且有一些重定向循环,我正在尝试我在网上找到的其他方法但没有成功.
I have a static site, a simple single page layout that I deploy in Dokku. I need to make a redirect (301) from www to non www and from *.website1.com
to website2.com
, but I don't know how. I was trying to use express-force-domain in npm but it makes my URL's crazy and having some redirect loops, I was trying other methods that I find on the web but none succeeds.
我的 server.js 是:
my server.js is:
var express = require('express')
var app = express();
app.set('port', (process.env.PORT || 80))
app.use(express.static(__dirname + '/public'))
app.get('/', function(request, response) {
response.send('Hello World!')
})
app.listen(app.get('port'), function() {
console.log("Node app is running at localhost:" + app.get('port'))
})
这是有效的版本,但没有重定向.
That is the version that works, but without redirects.
推荐答案
res.redirect(301, 'http://yourotherdomain.com' + req.path)
请参阅Express 文档.
这篇关于如何在 Node.js/Express 中进行重定向 (301)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文