UrlRewriter技术简单运用?!DOCTYPE urlrewrite PUBLIC -//tuckey.org//DTD UrlRewrite 2.6//EN???????
UrlRewriter技术简单运用
?
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"
??????? "http://tuckey.org/res/dtds/urlrewrite2.6.dtd">
<urlrewrite>
? <rule>
? <from>^/(\w+)/(\w+)/page_(\d+)\.html$</from>
? <to type="forward">/$1.htm?category=$2&page=$3</to>
</rule>
? <rule>
???? <from>^/rss/yahoo\.html$</from>
?????? <to type="redirect">??? [url]http://add.my.yahoo.com/rss? url=http://feed.feedsky.com/W3CSSiteFeed[/url]
????? </to>
</rule>
</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">??? [url]http://add.my.yahoo.com/rss? url=http://feed.feedsky.com/MySiteFeed[/url]
????? </to>
</rule>
?? 我们的网站用到最多的无非就这两种规则。至于正规表达式的写法很多种。希望对有感兴趣的朋友,可以试试。