2012-03-07 58 views

回答

4

返回一個301相應的位置,如果頭請求主機名== www.xyz.com

瀏覽器將做休息。

您可以在代理的任一側執行此操作。但我認爲這樣做是有道理的這樣做:

http.createServer(function (req, res) { 
    // 
    // Put your custom server logic here, then proxy 
    // 
    if (req.headers.host == 'www.xyz.com') { 
    res.writeHead(301, {'Location': 'xyz.com/' + req.url}); 
    res.end(); 
    } 
    else { 
    proxy.proxyRequest(req, res, { 
     host: 'localhost', 
     port: 9000 
    }); 
    } 

}).listen(80); 
+0

該路徑也應該重定向。主機頭只能包含主機名。我編輯我的帖子澄清。 – Alexander 2012-03-07 08:27:59

+0

301仍然可以工作,只需傳遞位置標題中的路徑信息,所有這些數據都應該在請求對象中可用。 – 2012-03-07 08:29:10

+1

謝謝。我不得不寫「res.writeHead(301,{'Location':'http://xyz.com/'+ req.url}); res.end();」而不是「res.send(...)」,因爲send()不存在。 – Alexander 2012-03-09 01:09:58