2012-02-28 103 views
4

我試圖使用Tuckey的urlRewriteFilter重寫任何網址爲https://,同時保留任何追加到URL的查詢字符串參數。我urlrewrite.xml文件目前看起來像Tuckey urlRewriteFilter use-query-string =「true」不工作?

<urlrewrite use-query-string="true"> 

<rule> 
    <note> 
     The rule means that requests to /test/status/ will be redirected to /rewrite-status 
     the url will be rewritten. 
    </note> 
    <from>/test/status/</from> 
    <to type="redirect">%{context-path}/rewrite-status</to> 
</rule> 

<rule match-type="regex"> 
    <condition type="header" operator="notequal" name="X-Forwarded-Proto">^HTTPS$</condition> 
    <condition type="request-uri" operator="notequal">/station/StationPingServlet</condition> 
    <condition type="request-uri" operator="notequal">/station/StudioPingServlet</condition> 
    <from>^.*$</from> 
    <to type="permanent-redirect" last="true">https://%{server-name}%{request-uri}</to> 
</rule> 

<outbound-rule> 
    <note> 
     The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url) 
     the url /rewrite-status will be rewritten to /test/status/. 

     The above rule and this outbound-rule means that end users should never see the 
     url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks 
     in your pages. 
    </note> 
    <from>/rewrite-status</from> 
    <to>/test/status/</to> 
</outbound-rule> 

我認爲使用查詢字符串= 「真」 將做到這一點,所以

http://server.com/test.jsp?company=3&id=1

將被改寫,以

https://server.com/test.jsp?company=3&id=1

但這似乎沒有發生。什麼情況是,

http://server.com/test.jsp?company=3&id=1

被改寫爲

https://server.com/test.jsp

難道我做錯了什麼?感謝您的任何建議。

回答

6

由於version 4.0,可能使用qsappend屬性爲to屬性。 qsappend的值默認爲false,所以您必須啓用它。

因此,解決辦法可以是,

<rule match-type="regex"> 
    <from>^.*$</from> 
    <to type="permanent-redirect" qsappend="true" last="true">https://%{server-name}%{request-uri}</to> 
</rule> 

UPDATE:對於低於4.0的版本,你可以在你的URL

<rule match-type="regex"> 
    <from>^.*$</from> 
    <to type="permanent-redirect" last="true">https://%{server-name}%{request-uri}?%{query-string}</to> 
</rule> 
+2

「qsappend」屬性僅在版本4.0中有效。 3.2中不存在。 比較http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd – 2012-10-02 15:43:47

+0

3.2的替代選擇是什麼? – gunnx 2015-01-29 16:35:55

+0

更新了答案。日Thnx。 – vtor 2015-02-19 14:39:09

0

的末尾使用?%{query-string}試試這個:

<rule> 
    <from>^(.*)$</from> 
    <to last="true" type="permanent-redirect">https://%{server-name}$1</to> 
</rule> 

務必添加'use-query-string="true"'on the urlrewrite標記

相關問題