您的位置:首页 > 编程语言 > Java开发

Java URLRewrite重定向

2014-07-07 00:04 330 查看
urlrewrite的主要作用:

    1)隐藏真实URL,提高安全性

    2)更加友好的URL,好记(看博客园就行知道啦)

    3)便于搜素引擎收录

首先,准备工作,下载urlrewrite:http://code.google.com/p/urlrewritefilter/downloads/list

可以随意选择自己喜好的版本下载。copy jar包到lib目录中,然后copy urlrewriter.xml到WEB-INF目录下,提供urlrewriter.xml文件内容:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"
"http://tuckey.org/res/dtds/urlrewrite2.6.dtd">

<!--

Configuration file for UrlRewriteFilter http://tuckey.org/urlrewrite/ 
-->
<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>

<!--

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.

-->

</urlrewrite>
添加 filter到web.xml:
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>logLevel</param-name><!--这里设置log日志级别 -->
<param-value>WARN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern> <!--拦截所有URL请求 --></span>
</filter-mapping>


PS:如果使用的是struts2框架, urlrewrite的filter需要在struts2的filter之前

urlrewrite中含有两个标签<rule>和<outbound-rule>,

<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>会把你的请求链接根据你写好的规则转换成对应的请求,也就是<rule>的子标签和<from>和<to>,<from>代表来源链接(伪链接),<to>代表转换后的连接(真实链接)。比如上面的请求,会把/test/status/转换成/rewrite-status请求服务器中内容。

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


<outbound-rule>就是把你页面上的URL根据规则转换对应的的美化后的URL,<outbound-rule> 需要重写URL,可以使用response.encodeURL  或者 JSTL<c:url>标签,<from>标签中的是真实链接,<to>中的是伪链接。这个标签会把你包含/rewrite-status的链接转换成/test/status/。

<from>和<to>中标签都是根据正则来进行匹配

<rule>
<from>^/company/([0-9]+).html$</from>
<to>/goods/company.jsp?companyId=$1</to>
</rule>
参数需要用()包围起来。[0-9]+代表一个或多个0-9的数字。

<from>标签中有两个小字符"^"和"$"。

“^”这个小字符,根据我的测试,代表你的项目URL,比如我的是http://localhost:8080/urlrewriteDemo/,如果你的请求是http://localhost:8080/urlrewriteDemo/company/1.html,则会被转换成http://localhost:8080/urlrewriteDemo/goods/company.jsp?companyId=1。

如果你的请求http://localhost:8080/urlrewriteDemo/test/company/1.html则不会被转换,因为匹配不上,如果去掉"^",则可以匹配上,请求会被转换成http://localhost:8080/urlrewriteDemo/test/goods/company.jsp?companyId=1。

"$"代表任意字符串,也就是说,你的请求字符串http://localhost:8080/urlrewriteDemo/company/1.html,无论在后面加什么字符串,比如说http://localhost:8080/urlrewriteDemo/company/1.html?id=1111,也只会转换成http://localhost:8080/urlrewriteDemo/goods/company.jsp?companyId=1,如果没有"$",则会被转换成http://localhost:8080/urlrewriteDemo/goods/company.jsp?companyId=1?id=1111
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息