大致需要改动几个地方和配置几个地方和注意几个地方
需要导入jar到classpath中两个一个urlrewriter-3.2.0jar 和urlrewriterfilter-4.0.3.jar (自己使用的)
然后就是配置web.xml 具体的我黏贴一下代码 然后再分析:
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<!-- set the amount of seconds the conf file will be checked for reload
can be a valid integer (0 denotes check every time,
empty/not set denotes no reload check) -->
<init-param>
<param-name>confReloadCheckInterval</param-name>
<param-value>0</param-value>
</init-param>
<!-- you can disable status page if desired
can be: true, false (default true) -->
<init-param>
<param-name>confPath</param-name>
<param-value>/WEB-INF/urlrewrite.xml</param-value>
</init-param>
<init-param>
<param-name>statusEnabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>logLevel</param-name>
<param-value>WARN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<!-- ueditor filter and struts 2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class> com.form.filter.EditorStrutsFilter </filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
先要配置url的filter拦截器 然后如果你使用的是struts 就是再配置struts拦截器 有先后之分如果还有其他的拦截器那也要放url栏拦截器的后面. 然后就是给struts配置 dispatcher
其中你会发现urlrewriter.xml的配置 .
配置urlreWriter.xml 时(要在webxml)说明路径 放在web-inf下
最后就是urlrewriter.xml 的书写> 先大致说一下 然后再具体说一下:
<?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">
<!--
Configuration file for UrlRewriteFilter
http://tuckey.org/urlrewrite/
^/goodsShow/([a-z]+)/([0-9]+)$
/goodsShow.xz?flag=$1&sign=$2
就比如这条规则,页面上的地址应该是:/goodsShow/abc/1
传到后台后就会被重写成:/goodsShow.xz?flag=abc&sign=1
-->
<urlrewrite>
<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>
<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>
<rule>
<note>catagory</note>
<from>^/([A-Za-z0-9_]+)$</from>
<to>/admin/catagory?slug=$1</to>
</rule>
<rule>
<note>comment-productId.html</note>
<from>^/([A-Za-z0-9_]+)$</from>
<to>/admin/post?slug=$1</to>
</rule>
<!--
<rule>
<note>post</note>
<from>^/([A-Za-z0-9_]+)$</from>
<to>/admin/post?slug=$1</to>
</rule>
-->
<!--
<rule>
<note>post</note>
<from>^/post/([a-z]+)$</from>
<to>/admin/postEdit?slug=$1</to>
</rule>
<rule>
<note>post2</note>
<from>/admin/postEdit?slug=$1</from>
<to>/admin/post</to>
</rule>
-->
<!--
INSTALLATION
in your web.xml add...
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>logLevel</param-name>
<param-value>WARN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
EXAMPLES
Redirect one url
<rule>
<from>/some/old/page.html</from>
<to type="redirect">/very/new/page.html</to>
</rule>
Redirect a directory
<rule>
<from>/some/olddir/(.*)</from>
<to type="redirect">/very/newdir/$1</to>
</rule>
Clean a url
<rule>
<from>/products/([0-9]+)</from>
<to>/products/index.jsp?product_id=$1</to>
</rule>
eg, /products/1234 will be passed on to /products/index.jsp?product_id=1234 without the user noticing.
Browser detection
<rule>
<condition name="user-agent">Mozilla/[1-4]</condition>
<from>/some/page.html</from>
<to>/some/page-for-old-browsers.html</to>
</rule>
eg, will pass the request for /some/page.html on to /some/page-for-old-browsers.html only for older
browsers whose user agent srtings match Mozilla/1, Mozilla/2, Mozilla/3 or Mozilla/4.
Centralised browser detections
<rule>
<condition name="user-agent">Mozilla/[1-4]</condition>
<set type="request" name="browser">moz</set>
</rule>
eg, all requests will be checked against the condition and if matched
request.setAttribute("browser", "moz") will be called.
-->
</urlrewrite>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN"
"http://tuckey.org/res/dtds/urlrewrite3.2.dtd">
<!--
Configuration file for UrlRewriteFilter
http://tuckey.org/urlrewrite/
^/goodsShow/([a-z]+)/([0-9]+)$
/goodsShow.xz?flag=$1&sign=$2
就比如这条规则,页面上的地址应该是:/goodsShow/abc/1
传到后台后就会被重写成:/goodsShow.xz?flag=abc&sign=1
-->
<urlrewrite>
<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>
<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>
<rule>
<note>catagory</note>
<from>^/([A-Za-z0-9_]+)$</from>
<to>/admin/catagory?slug=$1</to>
</rule>
<rule>
<note>comment-productId.html</note>
<from>^/([A-Za-z0-9_]+)$</from>
<to>/admin/post?slug=$1</to>
</rule>
<!--
<rule>
<note>post</note>
<from>^/([A-Za-z0-9_]+)$</from>
<to>/admin/post?slug=$1</to>
</rule>
-->
<!--
<rule>
<note>post</note>
<from>^/post/([a-z]+)$</from>
<to>/admin/postEdit?slug=$1</to>
</rule>
<rule>
<note>post2</note>
<from>/admin/postEdit?slug=$1</from>
<to>/admin/post</to>
</rule>
-->
<!--
INSTALLATION
in your web.xml add...
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>logLevel</param-name>
<param-value>WARN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
EXAMPLES
Redirect one url
<rule>
<from>/some/old/page.html</from>
<to type="redirect">/very/new/page.html</to>
</rule>
Redirect a directory
<rule>
<from>/some/olddir/(.*)</from>
<to type="redirect">/very/newdir/$1</to>
</rule>
Clean a url
<rule>
<from>/products/([0-9]+)</from>
<to>/products/index.jsp?product_id=$1</to>
</rule>
eg, /products/1234 will be passed on to /products/index.jsp?product_id=1234 without the user noticing.
Browser detection
<rule>
<condition name="user-agent">Mozilla/[1-4]</condition>
<from>/some/page.html</from>
<to>/some/page-for-old-browsers.html</to>
</rule>
eg, will pass the request for /some/page.html on to /some/page-for-old-browsers.html only for older
browsers whose user agent srtings match Mozilla/1, Mozilla/2, Mozilla/3 or Mozilla/4.
Centralised browser detections
<rule>
<condition name="user-agent">Mozilla/[1-4]</condition>
<set type="request" name="browser">moz</set>
</rule>
eg, all requests will be checked against the condition and if matched
request.setAttribute("browser", "moz") will be called.
-->
</urlrewrite>
其中需要用到通配符和正则表达式 这两种方法可以通过rule 中的属性来表明:具体看官方实例:
还有需要注意的就是如何正则表达式的匹配:
其中需要<to></to> <from></from> 就是访问谁 实际访问的地址 其中这两个地址 可以互相传递参数 (就是他的值可以被另个地址引用) 具体参数的使用看文档 .我配置的xml 也有少量示例.