URL重写

一,下载一个urlrewritefilter-4.0.3.jar ,拷贝到 WEB-INF/lib下面

 

二,修改web.xml,加入以下过滤器

<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>

 

三,在WEB-INF下面建立urlrewrite.xml,写入规则

<?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">
<!--
    Configuration file for UrlRewriteFilter
    http://www.tuckey.org/urlrewrite/
-->
<urlrewrite>
	<rule>
		<note>
			<![CDATA[
			通用格式(无参数)
			/namespace/method.html
			转为
			/namespace/method.action
			]]>
		</note>
		<from>^/([a-zA-Z0-9\.]+)/(\w+).html$</from>
		<to>/$1/$2.action</to>
	</rule>
	
	<rule>
		<note>
		    <![CDATA[
			通用格式(有一个参数)
			/namespace/methodName/paramName-paramValue.html
			转为
			/namespace/methodName.action?paramName=paramValue
		   ]]>
		</note>
		<from>^/([a-zA-Z0-9]+)/(\w+)/([a-zA-Z0-9\.]+)-([\w\,\.\|]+).html$</from>
		<to>/$1/$2.action?$3=$4</to>
	</rule>
	
	<rule>
		<note>
			<![CDATA[
			通用格式(有两个个参数)
			/namespace/methodName/paramName-paramValue/paramName2-paramValue2.html
			转为
			/namespace/methodName.action?paramName=paramValue¶mName2=paramValue2
			]]>
		</note>
		<from>^/([a-zA-Z0-9]+)/(\w+)/([a-zA-Z0-9\.]+)-([\w\,\.\|]+)/([a-zA-Z0-9\.]+)-([\w\,\.\|]+).html$</from>
		<to>/$1/$2.action?$3=$4&$5=$6</to>
	</rule>
	
	<rule>
		<note>
			<![CDATA[
			通用格式(有三个个参数)
			/namespace/methodName/paramName-paramValue/paramName2-paramValue2.html
			转为
			/namespace/methodName.action?paramName=paramValue¶mName2=paramValue2¶mName3=paramValue3
			]]>
		</note>
		<from>^/([a-zA-Z0-9]+)/(\w+)/([a-zA-Z0-9\.]+)-([\w\,\.\|]+)/([a-zA-Z0-9\.]+)-([\w\,\.\|]+)/([a-zA-Z0-9\.]+)-([\w\,\.\|]+).html$</from>
		<to>/$1/$2.action?$3=$4&$5=$6&$7=$8</to>
	</rule>
	
	<rule>
		<note>
			<![CDATA[
			通用格式(有四个个参数)
			]]>
		</note>
		<from>^/([a-zA-Z0-9]+)/(\w+)/([a-zA-Z0-9\.]+)-([\w\,\.\|]+)/([a-zA-Z0-9\.]+)-([\w\,\.\|]+)/([a-zA-Z0-9\.]+)-([\w\,\.\|]+)/([a-zA-Z0-9\.]+)-([\w\,\.\|]+).html$</from>
		<to>/$1/$2.action?$3=$4&$5=$6&$7=$8&$8=$9</to>
	</rule>
	
	<outbound-rule>
		<from>(\w+).action$</from>
		<to>$1.html</to>
	</outbound-rule>
	
	<outbound-rule>
		<from>(\w+).action\?([a-zA-Z0-9]+)\=([\w\,\.\|]+)$</from>
		<to>$1/$2-$3.html</to>
	</outbound-rule>
	
	<outbound-rule>
		<from>(\w+).action\?([a-zA-Z0-9]+)\=([\w\,\.\|]+)&([a-zA-Z0-9]+)\=([\w\,\.\|]+)$</from>
		<to>$1/$2-$3/$4-$5.html</to>
	</outbound-rule>
	
	<outbound-rule>
		<from>(\w+).action\?([a-zA-Z0-9]+)\=([\w\,\.\|]+)&([a-zA-Z0-9]+)\=([\w\,\.\|]+)&([a-zA-Z0-9]+)\=([\w\,\.\|]+)$</from>
		<to>$1/$2-$3/$4-$5/$6-$7.html</to>
	</outbound-rule>
	
	<outbound-rule>
		<from>(\w+).action\?([a-zA-Z0-9]+)\=([\w\,\.\|]+)&([a-zA-Z0-9]+)\=([\w\,\.\|]+)&([a-zA-Z0-9]+)\=([\w\,\.\|]+)&([a-zA-Z0-9]+)\=([\w\,\.\|]+)$</from>
		<to>$1/$2-$3/$4-$5/$6-$7/$8-$9.html</to>
	</outbound-rule> 
</urlrewrite>


 

你可能感兴趣的:(URL重写)