利用urlrewriter实现页面伪静态
来源:互联网 发布:剑灵路飞捏脸数据图 编辑:程序博客网 时间:2024/06/05 03:07
一个项目在后期运行的过程中,往往有各种理由去做页面的url地址的更换,给用户一种静态页面的感觉,其中利用到的技术主要有伪静态和真静态,其中真静态又根据存储位置的不同而会有两种方式,一种是定期生产html文件存储在磁盘上,而另外一种情况则是存储在内存中,在请求的时候直接从内存中取数据。
在这主要说的是伪静态,意思就是原本的动态页面,比如jsp,action等页面,但在用户的浏览器里看到的并不是以jsp结尾的,而是自定义的方式,比如htm或shtml等等。这里利用到的就是urlrewriter,http://tuckey.org/urlrewrite/
里面有详细的使用说明。
首先把urlrewrite-3.2.0.jar加入到项目的classpath中
然后在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>
最后在WEB-INF目录下建一个urlrewrite.xml文件,内容如下:
- <?xml version="1.0" encoding="utf-8" ?>
- <!DOCTYPE urlrewrite (View Source for full doctype...)>
- - <!--
- Configuration file for UrlRewriteFilter
- http://www.tuckey.org/urlrewrite/
- -->
- - <urlrewrite use-query-string="false" use-context="false">
- - <rule enabled="true">
- <note>The rule means that requests to /test/status/ will be redirected to /rewrite-status the url will be rewritten.</note>
- <from casesensitive="false">/test/status/</from>
- <to type="redirect" last="false" qsappend="false">%{context-path}/rewrite-status</to>
- </rule>
- - <outbound-rule enabled="true" encodefirst="false">
- <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 casesensitive="false">/rewrite-status</from>
- <to type="forward" last="false" qsappend="false">/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.
- Centralised browser detection
- <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>
<from></from>
<to type="forward></to>
</rule>
<from></from>写上你自己定义的访问地址,<to type="forward></to>就是实际的访问地址。比如我们实际的访问地址是:http://yousite.com/entity.htm ?category=user&page=2.而我们想把它重写为http://yousite.com/entity/uesr/page_2.html。这样看起来比我们实际的要好看的多。我们就应该这样的写:
<rule>
<from>^/(/w+)/(/w+)/page_(/d+)/.html$</from>
<to type="forward">/$1.htm?category=$2&page=$3</to>
</rule>
简单的介绍一下常用的正规表示式:
代码 说明
. 匹配除换行符以外的任意字符
/w 匹配字母或数字或下划线或汉字
/s 匹配任意的空白符
/d 匹配数字
/b 匹配单词的开始或结束
^ 匹配字符串的开始
$ 匹配字符串的结束
常用的&要用 &来表示。$1,$2代表与你配置正规表达式>/(/w+)/(/w+)/相对应的参数。<to type="forward">默认的是 type="forward".
另一个常用的规则就是连接外部的网站。就要用到。<to type="redirect">
<rule>
<from>^/rss/yahoo/.html$</from>
<to type="redirect"> http://add.my.yahoo.com/rss? url=http://feed.feedsky.com/MySiteFeed
</to>
</rule>
- 利用urlrewriter实现页面伪静态
- 利用urlrewriter实现页面伪静态
- 利用Mircosoft的URLRewriter.dll实现页面伪静态
- 利用Mircosoft URLRewriter.dll实现页面伪静态
- 利用Mircosoft URLRewriter.dll实现页面伪静态
- 利用Mircosoft URLRewriter.dll实现asp.net页面伪静态
- 利用Mircosoft URLRewriter.dll实现页面伪静态
- URLRewriter实现伪静态
- 利用Mircosoft URLRewriter.dll实现asp.net页面伪静态 (转)
- 利用Intelligencia.UrlRewriter.dll实现URL重写(伪静态)
- 利用 UrlRewriter 实现 ASP.NET 伪静态详解
- 利用URLRewriter重写url地址-实现伪静态
- .NET完美实现伪静态页面,URLRewriter.dll下载
- .NET完美实现伪静态页面,URLRewriter.dll下载
- .NET完美实现伪静态页面,URLRewriter.dll下载
- .NET完美实现伪静态页面,URLRewriter.dll下载
- urlrewriter实现jsp页面的伪静态化
- urlrewriter实现jsp页面的伪静态化
- UIImagePickerController
- uva 10635 - Prince and Princess(线段树)
- EXCEPTION_DEBUG_INFO structure
- 题目1127:简单密码
- poj2187
- 利用urlrewriter实现页面伪静态
- sqlarchemy操作
- 淘宝相关大牛、技术、开源
- GDB note
- Keil 不能查找字符的一种可能解决办法
- HTML5画布半圆教程
- JavaScript精简代码 非一般的写法
- Maven系列--序
- HTML5画布椭圆形教程