diff --git a/lib/http-proxy/passes/web-incoming.js b/lib/http-proxy/passes/web-incoming.js index 572c11a..e8e8419 100644 --- a/lib/http-proxy/passes/web-incoming.js +++ b/lib/http-proxy/passes/web-incoming.js @@ -144,7 +144,7 @@ web_o = Object.keys(web_o).map(function(pass) { proxyReq.on('response', function(proxyRes) { if(server) { server.emit('proxyRes', proxyRes, req, res); } for(var i=0; i < web_o.length; i++) { - if(web_o[i](req, res, proxyRes)) { break; } + if(web_o[i](req, res, proxyRes, options)) { break; } } // Allow us to listen when the proxy has completed diff --git a/lib/http-proxy/passes/web-outgoing.js b/lib/http-proxy/passes/web-outgoing.js index 9281c4a..52e6c82 100644 --- a/lib/http-proxy/passes/web-outgoing.js +++ b/lib/http-proxy/passes/web-outgoing.js @@ -1,5 +1,6 @@ -var passes = exports; - +var url = require('url') +var passes = exports; +var redirectRegex = /^30(1|2|7|8)$/; /*! * Array of passes. * @@ -43,6 +44,13 @@ var passes = exports; } }, + function setRedirectHostRewrite(req, res, proxyRes, options) { + if (options.hostRewrite && redirectRegex.test(proxyRes.statusCode)) { + var u = url.parse(proxyRes.headers['location']); + u.host = options.hostRewrite; + proxyRes.headers['location'] = u.format(); + } + }, /** * Copy headers from proxyResponse to response * set each header in response object.