UrlRewrite概念原理及使用方法解析 |
|
本文标签:UrlRewrite,概念,原理 URL Rewrite即URL重写,就是把传入Web的请求重定向到其他URL的过程 。URL Rewrite最常见的应用是URL伪静态化,是将动态页面显示为静态页面方式的一种技术 。比如http://www.123.com/news/index.asp?id=123 使用UrlRewrite转换后可以显示为 http://www.123.com/news/123.html URL Rewrite有什么用? 1,首先是满足观感的要求 。 2,其次可以隐藏网站所用的编程语言,还可以提高网站的可移植性 。 3,最后也是最重要的作用,是有利于搜索引擎更好地抓取你网站的内容 。 Java方面,参考使用:UrlRewriteFilter,地址:http://tuckey.org/urlrewrite/ 。 官方简介:A Java Web Filter for any compliant web application servers (such as Tomcat, JBoss, Jetty or Resin), which allows you to rewrite URLs before they get to your code. It is a very powerful tool just like Apache's mod_rewrite! 1.增加Jar包urlrewritefilter-4.0.3.jar到Lib 2.在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> 关于配置的更多信息点击这里! 3.增加urlrewrite.xml到你的WEB-INF,点击查看示例 。 这里为了示例,我写了两个功能的节点配置:
<?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">
<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}/index.jsp</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>
</urlrewrite>
index.jsp页面内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<c:url var="myURL" value="/rewrite-status" />
<a href="${myURL }" rel="external nofollow" >AAAAA</a>
</body>
</html>
Note已经说的很清楚 第一个功能是转换,当请求 /test/status/ 时实际请求到的是index.jsp 第二个功能是页面显示URL的转换,这里必须使用JSTL c:url,将value部分转换为指定路径,达到屏蔽URL的功能 4.实际效果 当请求 /test/status/ 时实际请求到的是index.jsp <html> <body> <a href="/f/test/status/" rel="external nofollow" >AAAAA</a> </body> </html> 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家 。 |