diff --git a/lib/node-http-proxy/http-proxy.js b/lib/node-http-proxy/http-proxy.js index 2528a02c4..24183943c 100644 --- a/lib/node-http-proxy/http-proxy.js +++ b/lib/node-http-proxy/http-proxy.js @@ -27,6 +27,7 @@ var events = require('events'), http = require('http'), util = require('util'), + url = require('url'), httpProxy = require('../node-http-proxy'); // @@ -122,7 +123,8 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) { var self = this, errState = false, outgoing = new(this.target.base), - reverseProxy; + reverseProxy, + location; // // Add common proxy headers to the request so that they can @@ -257,11 +259,14 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) { if ((response.statusCode === 301) || (response.statusCode === 302) && typeof response.headers.location !== 'undefined') { - if (self.source.https && !self.target.https) { - response.headers.location = response.headers.location.replace(/^http\:/, 'https:'); - } - if (self.target.https && !self.source.https) { - response.headers.location = response.headers.location.replace(/^https\:/, 'http:'); + location = url.parse(response.headers.location); + if (location.host === req.headers.host) { + if (self.source.https && !self.target.https) { + response.headers.location = response.headers.location.replace(/^http\:/, 'https:'); + } + if (self.target.https && !self.source.https) { + response.headers.location = response.headers.location.replace(/^https\:/, 'http:'); + } } }