urlrewrite的rule怎么写

一、正则表达式教程

常用正则如下:

.              换行符以外的所有字符

\w          匹配字母或数字或下划线或汉字

\s           匹配任意的空白符

\d          匹配数字

\b          匹配单词的开始或结束

^            匹配字符串的开始

$            匹配字符串的结束

*             重复零次或更多次

+            重复一次或更多次

?           重复零次或一次

{n}          重复n次

{n,}      重复n次或更多次

{n,m}   重复n到m次

 

更多的正则表达式说明,以及学习可以去手册中看

 

 

 

 

应用替换时,前面一个()中匹配的内容后面就用$1引用,第二个()中匹配的就用$2引用,这个()里面的东西叫原子组

 

分析一下.htaccess里面的重写:

  RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2

 

  首先加入用户通过 linuxidc.com/forum-2-3.html 访问网站,那么先通过.htaccess过滤,看看是否需要.htaccess引导一下用户,如果满足列出的一系列RewriteCond的 条件那么就进行重写。

 

  forum-2-3.html 这个正好符合列出的 ^forum-([0-9]+)-([0-9]+)\.html$,所以开始进行转写。

  正则表达式。并且 $1 为 2 ,$2为3 ,

  所以输入后,即 forumdisplay.php?fid=2&page=3 加上前面的RewriteBase 指定的文件目录,那么就带他到制定目录的forumdisplay.php?fid=2&page=3 。

 

二、常见的.htaccess应用举例(部分例子引自四个例子实战讲解.htaccess文件rewrite规则)

  1》 防止盗链,如果来得要访问jpe jpg bmp png结尾的url 用户不是来自我们的网站,那么让他看一张我们网站的展示图片。

   RewriteEngine On

        RewriteCond %{HTTP_REFERER} !^http://(.+.)?mysite.com/ [NC]

        RewriteCond %{HTTP_REFERER} !^$

        RewriteRule .*.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]

 

  2》 网站升级的时候,只有特定IP才能访问,其他的用户将看到一个升级页面

  RewriteEngine on

       RewriteCond %{REQUEST_URI} !/upgrade.html$

       RewriteCond %{REMOTE_HOST} !^24\.121\.202\.30

  RewriteRule $ http://www.linuxidc.com/upgrade.html [R=302,L]

 

  3》 把老的域名转向新域名

  # redirect from old domain to new domain

       RewriteEngine On

       RewriteRule ^(.*)$ http://www.yourdomain.com/$1[R=301,L]

 

三、常用示例:

 

    示例1:http://www.yf.com/index.html -> http://www.yf.com/index.php:

    RewriteEngine On

    RewriteRule index.html index.php

 

    示例2:http://www.yf.com/test8.html -> http://www.yf.com/test.php?id=8

    RewriteEngine On

    RewriteRule ^test([0-9]*).html$ test.php?id=$1

 

    示例3:http://www.yf.com/cat-1-3.html -> http://www.yf.com/cat.php?id1=1&id2=3        

    RewriteEngine On

    RewriteRule ^cat-([0-9]+)-([0-9]+)\.html$ cat.php?id1=$1&id2=$2

  

     示例4:http://y.com/cat-zbc2ac-3-5.html -> http://y.com/cat.php?id0=zbc2ac&id1=3&id2=5  

    RewriteEngine On

    RewriteRule ^cat-([a-zA-Z0-9\-]*)-([0-9]+)-([0-9]+)\.html$ cat.php?id0=$1&id1=$2&id2=$3

 

    示例5:http://www.yf.com/cat1-4-3-8.html -> http://www.yf.com/cat1.php?id1=4&id2=3&id3=8

    RewriteEngine On

    RewriteRule ^cat1-([0-9]+)-([0-9]+)-([0-9]+)\.html$ cat1.php?id1=$1&id2=$2&id3=$3

 

    示例6:http://www.yzzmf.com/cat5/ -> http://www.yzzmf.com/cat.php?id1=5

    RewriteEngine On

    RewriteRule ^cat([0-9]*)/$ cat.php?id1=$1

 

    示例7:http://www.yzzmf.com/catm6/3/ -> http://www.yzzmf.com/catm.php?id1=6&id2=3

    RewriteEngine On

    RewriteRule ^catm([0-9]*)/([0-9]*)/$ catm.php?id1=$1&id2=$2

 

    其中不提倡贪婪模式,把0-9都改成([\d]*?)     *?允许空

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值