2015-12-11 78 views
0

我有一個嵌入式Jetty服務器,我想直接從http://localhost:8080/https://localhost:4433/請求我該怎麼做?我試過這個:Jetty使用重寫將HTTP重定向到HTTPS

// Set up rewriting (HTTP -> HTTPS) 
    RewriteHandler rewrite = new RewriteHandler(); 

    RedirectPatternRule redirect = new RedirectPatternRule(); 
    redirect.setPattern("http://localhost:8080/*"); 
    redirect.setLocation("https://localhost:4433/"); 
    rewrite.addRule(redirect); 

並將其添加到我的處理程序中,但它不起作用。我知道規則就像

RewriteHandler rewrite = new RewriteHandler(); 

    RedirectPatternRule redirect = new RedirectPatternRule(); 
    redirect.setPattern("/redirect/*"); 
    redirect.setLocation("/redirected"); 
    rewrite.addRule(redirect); 

工作100%,但有沒有辦法匹配完整的URL?

回答

0

一個更普遍的做法是不特定於碼頭是一個新的URL返回一個301:

GET /path/location.html HTTP/1.1 
Host: example.com 

被變成

HTTP/1.1 301 Moved Permanently 
Location: https://example.com/path/location.html 

這可以在Jetty和大多數服務器框架中完成,只需設置響應狀態並在過濾器中添加位置標題(如果y你不希望資源通過純文本http訪問。