分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

伪静态URLRewrite学习

发布时间:2023-09-06 01:35责任编辑:赖小花关键词:暂无标签

UrlRewrite:

UrlRewrite就是我们通常说的地址重写,用户得到的全部都是经过处理后的URL地址,类似于Apache的mod_rewrite。将我们的动态网页地址转化为静态的地址,如html、shtml,还可以隐藏网页的真正路径,

比如:有时候需要将xxx.com/news/ type1/001.jsp 转化成显示路径为xxx.com/news_type1_001.html

 

有点如下:

 

一:提高安全性,屏蔽内部的url结构.

 

二:美化URL

 

三:更有利于搜索引擎的收入,通过对URL的一些优化,可以使搜索引擎更好的识别与收录网站的信息.

 

下载地址: 官网下载: http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/4.0/index.html#filterparams
实例展示

 

实例应用版本urlrewritefilter-4.0.3. Tomcat服务器端口定制为80

 

1. 创建web项目,增加 urlrewritefilter-4.0.3.jar 到 WEB-INF/lib 

 

2. 在WEB-INF/web.xml 增加urlrewritefilter过滤器 (near the top above any servlet mappings)

 

  
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" ???xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ???xsi:schemaLocation="http://java.sun.com/xml/ns/javaee ????http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> ???<!-- 加到任何servlet映射的顶部,不然可能有些路径不能被过滤到 ????????http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/3.2/index.html ????--> ???<filter> ???????<filter-name>UrlRewriteFilter</filter-name> ???????<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> ???????<!-- ????????????设备文件重加载间隔 (0默示随时加载, -1默示不重加载, 默认-1) ????????--> ???????<init-param> ???????????<param-name>confReloadCheckInterval</param-name> ???????????<param-value>60</param-value> ???????</init-param> ???????????????<!-- 自定义配置文件的路径,是相对context的路径,(默认位置 /WEB-INF/urlrewrite.xml) --> ???????<init-param> ???????????<param-name>confPath</param-name> ???????????<param-value>/WEB-INF/urlrewrite.xml</param-value> ???????</init-param> ???????????????<!-- ????????????设置日志级别(将被记录到日志中) ??????????????可以为: TRACE, DEBUG, INFO (default), WARN, ERROR, FATAL, log4j, commons, slf4j, ??????????????比如 sysout:DEBUG(设置到控制台调试输出级别) ????????????(默认级别 WARN) --> ???????<init-param> ???????????<param-name>logLevel</param-name> ???????????<param-value>DEBUG</param-value> ???????</init-param> ???</filter> ???????<filter-mapping> ???????<filter-name>UrlRewriteFilter</filter-name> ???????<url-pattern>/*</url-pattern> ???????<dispatcher>REQUEST</dispatcher> ???????<dispatcher>FORWARD</dispatcher> ???</filter-mapping> ???????????<welcome-file-list> ???????<welcome-file>index.jsp</welcome-file> ???</welcome-file-list></web-app>
 

 

如果觉得/*这样的通配,并不符合我的预期,我只想对部分路径进行URL的重写,/*可能会造成我想象不到的或者是许微不足道的性能浪费.我把它改成了我需要的:

 

  
 ???<filter-mapping> ???????<filter-name>UrlRewriteFilter</filter-name> ???????<url-pattern>/member/*</url-pattern> ???</filter-mapping> ???<filter-mapping> ???????<filter-name>UrlRewriteFilter</filter-name> ???????<url-pattern>/article/*</url-pattern> ???</filter-mapping>
 

 

更多请参考: http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/3.2/index.html

 

3. 因为上面我们通过confPath定义了配置文件的路径,其实该默认位置就是在/WEB-INF/urlrewrite.xml,为了更能说明问题,所以显示指定下

 

  
<?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> ???????<from>/page/(.*).html</from> ?????????<to>/index.jsp?page=$1</to> ????</rule> ???????<rule> ???????<from>^/user/([a-z]+)/([0-9]+)$</from> ???????<to>/index.jsp?nickname=$1&amp;age=$2</to>  </rule></urlrewrite>
 

 

此时我们就可以通过url进行模拟了.

 

注意:

 

1.urlrewrite.xml是utf-8.所以如果你要在rule上加note标签为中文的话,也一定是要utf-8.

 

2.UrlRewriteFilter 最好是配置在web.xml的前面filter上,不然有可能对有些url转变失去作用.

 

3.urlrewrite属性:有仅只有一个,rule属性::至少一个.

 

4.在写rule的时,如果有多个参数时,中间的连接符号&应该是&

 

5.rule是url重写规则,from是显示出来的地址,to是映射的实际地址,$1是重写参数,它的值与from中的正则表达式是一一对应,可以为多个,()里是匹配的正则表达式, 在正则表达式^指定字符的串开始,$为指定结束

 

6.对于中文参数要使用(.*)作为参数转义.

 

4.重写url演示

 

实例1

 

  
<rule> ???????<from>/page/(.*).html</from> ?????????<to>/index.jsp?currentPage=$1</to> </rule>
 

 

index.jsp中的内容

 

  
 ?<body> ?????????<% ?????????????String current = request.getParameter("currentPage"); ??????????%> ???????????????????当前页码<%=current %> ?</body>
 

 

执行效果如下:

 

 

实例2

 

Rule规则

 

  
<rule> ???????<name>World Rule</name> ???????<from>^/user/([a-z]+)/([0-9]+)$</from> ???????<to>/index.jsp?nickname=$1&amp;age=$2</to></rule>
 

 

index.jsp中的内容

 

  
<body> ?????????<% ?????????????String username = request.getParameter("nickname"); ?????????????int age = Integer.parseInt(request.getParameter("age")); ??????????%> ???????????????????用户名: <%=username %> 年龄: <%=age %> <br></body>
 

 

执行效果如下:

 

 

所以,当我们在url中输入”http://localhost/urlrewrite/user/dennisit/23”时,实际执行的就是”http://localhost/urlrewrite/index.jsp?nickname=dennisit&age=23”

 

实例3

 

同理rule规则如下时

 

  
<rule> ???????<from>^/page/(.*)$</from> ???????<to type="redirect">/page/$1.action</to></rule>
 

 

这样我访问的:http://localhost/urlrewrite/page/test

 

则跳转到:    http://localhost/urlrewrite/page/test.action

 

实例4 Rule规则

 

  
 ???<rule> ???????<from>^/([a-z]+)/([a-z]+)/([a-z]+)$</from> ???????<to>/$1.do?method=$2&amp;uuid=$3</to> ???</rule>
 

 

在index.jsp中添加如下链接:

 

  
 ???<a href="process/show/index">跳转</a>
 

 

当点击该链接,

 

地址栏中显示url是:http://localhost/urlrewrite/process/show/index,

 

其实际执行路径是:http://localhost/urlrewrite/process.do?method=show&uuid=index

 

转至:【http://www.cnblogs.com/dennisit/p/3177108.html】

 

 

伪静态URLRewrite学习

原文地址:https://www.cnblogs.com/lengxiaofeng/p/8267508.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved