如何创建 http 将端口 80 上的 http 请求重定向到端口 443 上的 https?
How do create an http redirect an http request coming in on port 80 to https on port 443?
我想从像 http://whatever.com/whatever?whatever to https://whatever.com/whatever?whatever 这样的 url 创建状态 301 重定向。我正在使用 node.js 但我怀疑答案是 node-specific.
我知道你可以像这样用 url 写 "Location" header:
response.writeHead(301, {
'Location': request.url
});
response.end();
但是如何指定我希望重定向转到 https?
通常为此目的使用 301 永久重定向。要更改协议,您需要在 Location
header:
中包含协议和主机
var server = http.createServer(function(request, response){
var newUrl = 'https://' + request.headers.host + request.url;
response.writeHead(301, {
'Location': newUrl
});
response.end();
});
标准的 Node HTTP 包不会自动解析主机名和端口,所以如果你需要与非标准端口兼容,你应该使用像 Express 这样的包来轻松获取 req.hostname
.
我想从像 http://whatever.com/whatever?whatever to https://whatever.com/whatever?whatever 这样的 url 创建状态 301 重定向。我正在使用 node.js 但我怀疑答案是 node-specific.
我知道你可以像这样用 url 写 "Location" header:
response.writeHead(301, {
'Location': request.url
});
response.end();
但是如何指定我希望重定向转到 https?
通常为此目的使用 301 永久重定向。要更改协议,您需要在 Location
header:
var server = http.createServer(function(request, response){
var newUrl = 'https://' + request.headers.host + request.url;
response.writeHead(301, {
'Location': newUrl
});
response.end();
});
标准的 Node HTTP 包不会自动解析主机名和端口,所以如果你需要与非标准端口兼容,你应该使用像 Express 这样的包来轻松获取 req.hostname
.