我想要创建一个状态301,从像http://whatever.com/whatever?whatever这样的url重定向到https://whatever.com/whatever?whatever。我使用的是node.js,但我怀疑答案是特定于节点的。
我知道您可以用这样的url来编写"Location“标题:
response.writeHead(301, {
'Location': request.url
});
response.end();但是,如何指定要将重定向转到https?
发布于 2015-03-23 07:33:13
为此目的,通常使用301永久重定向。要更改协议,需要在Location头中包含协议和主机:
var server = http.createServer(function(request, response){
var newUrl = 'https://' + request.headers.host + request.url;
response.writeHead(301, {
'Location': newUrl
});
response.end();
});标准的Node包不会自动解析主机名和端口,因此如果您需要与非标准端口兼容,那么应该使用像快递这样的包轻松地获取req.hostname。
https://stackoverflow.com/questions/29204943
复制相似问题