Urlrewrite(url地址重写)和UrlRewriteFilter

7 篇文章 0 订阅
4 篇文章 0 订阅
Urlrewrite(url地址重写)和UrlRewriteFilter

Urlrewrite:

urlRewrite就是我们通常说的地址重写,用户得到的全部都是经过处理后的URL地址

UrlRewrite的含义:

优点

1、提高安全性,可以有效的避免一些参数名、ID等完全暴露在用户面前,如果用户随便乱输的话,不符合规则的话直接会返回个404或错误页面,这比直接返回500或一大堆服务器错误信息要好的多
2、:美化URL,去除了那些比如*.do之类的后缀名、长长的参数串等,可以自己组织精简更能反映访问模块内容的URL
3、:更有利于搜索引擎的收入,通过对URL的一些优化,可以使搜索引擎更好的识别与收录网站的信息

使用范围

地址重写一般是用于将动态地址伪静态。如果本身就是静态就没必要了。地址重写后网站制作者可以通过输入地址名直接访问。


UrlRewrite的使用:

urlrewrite其实就是使用UrlRewriteFilter来实现的,它将会过虑用户的所有请求,符合规则的便对其进行重定向。

UrlRewriteFilter使用步骤:

UrlRewriteFilter是一个用于改写URL的Web过滤器,类似于Apache的mod_rewrite。适用于任何Web应用服务器(如Resin,Orion,Tomcat等)。其典型应用就把动态URL静态
使用步骤(来自官网http://www.tuckey.org/urlrewrite/)(由于官网使用的是英文,本人英语水平有限,有任何错误,请大家指正,谢谢):
1、Add urlrewritefilter-4.0.3.jarto WEB-INF/lib(添加jar包到lib中),
如果使用maven,添加maven的依赖:
<dependency>
    <groupId>org.tuckey</groupId>
    <artifactId>urlrewritefilter</artifactId>
    <version>4.0.3</version>
</dependency>

2、在web.xml中添加如下代码:(注意:加入到所有servlet 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、在WEB-INFO下添加文件 urlrewrite.xml,在里面配置自己的请求
4、urlrewwiter.xml的配置文件如下:(简单举例,如果要查看更复杂的url地址重写规则,请点击:http://tuckey.org/urlrewrite/manual/3.0/guide.html)
在url中的配置主要有:
在这个标签的配置中:name和note都可以省略,from和to不能省略!并且:to的属性type默认情况下为转发forward
<rule>
<name>(这里是规则名称)</name>
<note>(规则的注释,说明规则是干什么的)</note>
<from>(请求的地址)</from>
<to type="redirect">(重写后的地址)</to>
</rule>
<outbound-rule>
<name></name>
<note></note>
<from></from>
<to></to>
</outbound-rule>
<?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 decode-using="utf-8">
<rule>
<!--在你在地址栏中输入http://localhost:8080/(应用名)/commonTemplateFile/degmb/degmb/index.html-->
<!--或者在你在地址栏中输入http://localhost:8080/(应用名)/commonTemplateFile/ggg/index.html-->
<!--或者在页面href中-->
<from>^/commonTemplateFile/(.*)\.html$</from>
<!--会将地址http://localhost:8080/(应用名)/commonTemplateFile/ggg/index.html重写http://localhost:8080/(应用名)/XXX.do?method=JJJ&path=/ggg 在方法中就可以获得参数值了-->
<to>/XXX.do?method=JJJ&amp;path=$1</to>
</rule>
<rule>
<note>
<!--这个规则定义的是:对/test/status/这个的请求,将会被重定向到对这个请求/rewrite-status重写的地址-->
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>
<!--outbound-rule定义的是:当response.encodeURL被调用(如果你使用的是JSTL标签c:url),那么url地址/rewrite-status将会被重写为/test/status/。上面的rule和这个outbound-rule意味着最后用户不会看见/rewrite-status这个地址,只会看见/test/status/在地址栏或者页面的超链接中-->
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>
</urlrewrite>

参考文章:urlwriter百度百科、urlwriterfilter百度百科以及http://tuckey.org/urlrewrite/manual/3.0/guide.html)
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值