urlrewirte基本配置與常見問題.

      項目中用到urlrewrite來僞靜態化url地址,故對urlrewrite進行了簡單的研究應用。在此把基本配置和常見的問題記錄下來,以備後用。

 

      urlrewritefilter簡介 :是一個用於改寫url的web過濾器,類似於Apache的mod_rewrite,適用於任何web服務器(tomcat,jboss,jetty等),他是一個把url進行僞靜態化的開源工具包,並不是真正的靜態化。典型的應用就是把weburl靜態化,以便於搜索引擎爬蟲抓取你的頁面。

 

      引入urlrewrite的目的:

      1):爲了對搜索友好,因爲搜索進行對動態url抓取沒有對靜態url高。

      2):屏蔽內部url結構,使應用相對安全,提高網站的可移植性。

      3):在一定成度上也對url地址有美化和從簡的作用。

 

      簡單示例

 

http://www.xxx.net/user/profile.do?id=20001====>http://www.xxx.net/user/20001
http://www.xxx.net/forum/board.do?name=jav ====>http://www.xxx.net/forum/java
http://www.xxx.net/forum/thread.do?id=29923 ====>http://www.xxx.net/thread/29923 
 

 

      urlrewritefilter基本配置步驟

      1):下載urlrewrite包,並把他考到項目的WEB-INF/lib目錄下。

               注:如果項目中使用了maven,並搭建了nexus數據倉庫,則可以直接通過nexus進行搜索,並選擇你需要的版本,把xml配置複製到你工程的pom.xml中即可。

 

      2):在WEB-INF目錄下添加rewrite.xml文件。rewrite.xml的dtd頭比較重要,如果dtd頭中的版本號與實際urlrewrite包版本不匹配的話會報如下錯誤:

 

org.tuckey.web.filters.urlrewrite.Conf ERROR:
Exception loading conf  Connection timed out:connect java.net.ConnectException:
Connection timed out: connect 

 

       3):在web.xml文件中添加urlrewritefilter過濾器:

 

<filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
 

       4):在rewrite.xml文件中配置你自己的url轉換規則,簡單示例如下:

 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN" "http://tuckey.org/res/dtds/urlrewrite3.2.dtd">
<urlrewrite> 
    <rule>
        <name>World Rule1</name>
        <note>註釋1</note>
        <from>^/newbie</from>
        <to type="forward">/newbie.jsp</to>
    </rule>
    <rule>
        <name>World Rule2</name>
        <note>註釋2</note>
        <from>^/self-service</from>
        <to type="forward">/self-service.jsp</to>
    </rule>
</urlrewrite>
 

      配置urlrewirte注意事項:

      1):urlrewirte.xml文件的編碼格式爲utf-8,如果<note>節點中有中文,那麼中文編碼也必須是utf-8編碼的。

      2):配置web.xml的時候,urlrewritefilter過濾器必須配置在struts2的攔截器之前,否則urlrewritefilter過濾器有可能對某些url不起作用。

      3):strtus2的filter-mapping一定要添加:<dispatcher>FORWARD</dispatcher>

      4):在寫rule的時,如果有多個參數時,中間的連接符號&應該是&amp;

      5):urlrewrite.xml標籤的一些說明:

                urlrewrite屬性:有僅只有一個.

                rule屬性::至少一個

 

     urlrewrite可能引起的錯誤:

      1):connect java.net.ConnectException異常

 

org.tuckey.web.filters.urlrewrite.Conf ERROR:
Exception loading conf  Connection timed out:connect java.net.ConnectException:
Connection timed out: connect 

      1-R:rewrite.xml配置文件的dtd頭有問題,打開urlrewrite包,查看相應的dtd版本信息。

 

      2):404錯誤:

      2-R:strtus2的filter-mapping一定要添加:<dispatcher>FORWARD</dispatcher>,或者在urlrewritefilter過濾器的filter-mapping中添加相應屬性,如下:

<filter>
    <filter-name>urlRewrite</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>urlRewrite</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
</filter-mapping>
 

    本文參考了網上很多朋友的相關文章,並結合自身的特點進行了簡單的彙總,在此謝謝這些朋友的文章。

    下面博客對urlrewrite進行了詳細研究:

    http://blog.csdn.net/lgg201/archive/2010/02/26/5329364.aspx

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章