url重写的好处:
1、保护真实url的结构组成,提高安全性
2、有利于搜索引擎的收入,提高搜索排名
java web中对url进行重写方法:
1.引入urlrewritefilter jar包
dependency><groupId>org.tuckey</groupId><artifactId>urlrewritefilter</artifactId><version>${urlrewrite.version}</version></dependency>
2.配置web.xml
<filter><filter-name>UrlRewriteFilter</filter-name><filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class><init-param><param-name>confPath</param-name><param-value>/WEB-INF/urlrewrite.xml</param-value></init-param><init-param><param-name>logLevel</param-name><param-value>INFO</param-value></init-param></filter><filter-mapping><filter-name>UrlRewriteFilter</filter-name><url-pattern>/item/*</url-pattern><dispatcher>REQUEST</dispatcher><dispatcher>FORWARD</dispatcher></filter-mapping><filter-mapping><filter-name>UrlRewriteFilter</filter-name><url-pattern>/article/*</url-pattern><dispatcher>REQUEST</dispatcher><dispatcher>FORWARD</dispatcher></filter-mapping><filter-mapping><filter-name>UrlRewriteFilter</filter-name><url-pattern>/activity/*</url-pattern><dispatcher>REQUEST</dispatcher><dispatcher>FORWARD</dispatcher></filter-mapping>
3.配置urlrewrite.xml文件,该文件默认在WEB-INF目录下
<!DOCTYPE urlrewrite PUBLIC "-////DTD UrlRewrite 4.0//EN""/res/dtds/urlrewrite4.0.dtd"><urlrewrite><rule><note>重写商品详情页链接</note><from>^/item/([0-9]+).html$</from><to>/item/itemDetail?id=$1</to></rule><rule><note>文章详情链接</note><from>^/([a-z]+)/([0-9]+).html$</from><to>/$1/detail?id=$2</to></rule></urlrewrite>
<note>注释说明
<from>表示静态url,即访问的来源,可以是正则匹配
<to>表示转向的目标url,项目中真实访问的url
$1 $2表示静态url中匹配的参数,依次对应
<to>标签默认的type的属性是forward,即浏览器中的地址不会发生改变;redirect值表示重定向,地址会发生改变。