Google Analytics

2014年10月4日 星期六

使用UrlRewriter重寫網址

簡介

UrlRewrite(http://tuckey.org/urlrewrite/)的作用是用來改寫網址,例如用在產生較短的網址、或配合SEO調整網址命名等。

使用方式

1. 複製urlrewritefilter-4.0.3.jar到WEB-INF/lib目錄下
2. 修改WEB-INF/web.xml檔案,增加urlrewite filter與filter-mapping
<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>
           <dispatcher>REQUEST</dispatcher>
           <dispatcher>FORWARD</dispatcher>

</filter-mapping>

3.新增urlrewrite.xml到WEB-INF目錄下
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
        "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<urlrewrite>
           <!-- 清除jsessionid -->
           <rule match-type="regex">
                     <from>(.*)(;jsessionid=.{32})(.*)</from>
                     <to type="forward">$1$3</to>
           </rule>

           <!-- transmit rule  -->
           <rule match-type="regex">
                     <condition type="request-uri" operator="equal ">.+(\.do).*</condition>
                     <condition type="request-uri" operator="notequal">.+(transmit\.do).*</condition>
                     <from>/(.+)</from>
                     <to type="forward">/transmit.do?forwardTo=/$1</to>
           </rule>
          

</urlrewrite>

以上述範例而言,筆者在urlrewrite.xml增加了2個rule,第一個rule是去除url上的jsessioid、大二個rule是將如http://localhost:8080/webapp/exmaple/hello.do的網址轉換成http://localhost:8080/webapp/transmit.do?forwardTo=/exmaple/hello.do,藉此產生較短網址。

沒有留言:

張貼留言