• PHP 伪静态规则写法RewriteRule-htaccess详细语法使用


    一、正则表达式教程

    伪静态规则写法RewriteRule-htaccess详细语法使用教程分享

    简单说下:伪静态实际上是利用PHP把当前地址解析成另外一种方法进行访问网站!要学伪静态规则的写法,你必须得懂一点正则,不会没关系,照着下面的套就行


    一、正则表达式教程

    有一个经典的教程:
    这个教程的确很简单,看完基本上写一些简单的正则就没有问题了。正则是一个需要长期使用的工具,隔段时间不用会忘记,所以我每次都看一遍这个教程。其实学过之后重要的就是一点内容。

    简单罗列如下:

    .换行符以外的所有字符
    w 匹配字母或数字或下划线或汉字
    s 匹配任意的空白符
    d 匹配数字
     匹配单词的开始或结束
    ^ 匹配字符串的开始
    $ 匹配字符串的结束
    * 重复零次或更多次
    + 重复一次或更多次
    ? 重复零次或一次
    {n} 重复n次
    {n,}重复n次或更多次
    {n,m} 重复n到m次

    应用替换时,前面第一个()中匹配的内容后面就用$1引用,第二个()中匹配的就用$2应用……
    这个个()里面的东东叫原子组
    分析一下 discuz搜索引擎优化 htaccess 里面的重写。

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

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

    discuz的没有列出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规则)

    4.1 防止盗链,如果来得要访问jpe jpg bmp png结尾的url 用户不是来自我们的网站,那么让他看一张我们网站的展示图片。
    RewriteEngine OnRewriteCond %{HTTP_REFERER} !^http://(.+.)?mysite.com/ [NC]RewriteCond %{HTTP_REFERER} !^$RewriteRule .*.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]

    4.2 网站升级的时候,只有特定IP才能访问,其他的用户将看到一个升级页面
    RewriteEngine onRewriteCond %{REQUEST_URI} !/upgrade.html$RewriteCond %{REMOTE_HOST} !^24.121.202.30

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

    4.3把老的域名转向新域名
    # redirect from old domain to new domainRewriteEngine OnRewriteRule ^(.*)$http://www.yourdomain.com/$1[R=301,L]

    三、常用示例

    RewriteEngine On
    RewriteRule index.html index.php
    
    比如:http://www.xxx.com/index.html  -> http://www.xxx.com/index.php
    
    RewriteRule ^test([0-9]*).html$ test.php?id=$1
    
    比如:http://www.xxx.com/test8.html  -> http://www.xxx.com/test.php?id=8
    
    RewriteRule ^cat-([0-9]+)-([0-9]+).html$ cat.php?id1=$1&id2=$2
    
    比如:http://www.xxx.com/cat-1-3.html -> http://www.xxx.com/cat.php?id1=1&id2=3
    
    RewriteRule ^cat-([a-zA-Z0-9-]*)-([0-9]+)-([0-9]+).html$ cat.php?id0=$1&id1=$2&id2=$3
    
    比如:http://www.xxx.com/cat-zbc2ac-3-5.html -> http://www.xxx.com/cat.php?id0=zbc2ac&id1=3&id2=5
    
    RewriteRule ^cat1-([0-9]+)-([0-9]+)-([0-9]+).html$ cat1.php?id1=$1&id2=$2&id3=$3
    
    比如:http://www.xxx.com/cat1-4-3-8.html -> http://www.xxx.com/cat1.php?id1=4&id2=3&id3=8
    
    RewriteRule ^cat([0-9]*)/$ cat.php?id1=$1
    
    比如:http://www.xxx.com/cat5/ -> http://www.xxx.com/cat.php?id1=5
    
    RewriteRule ^catm([0-9]*)/([0-9]*)/$ catm.php?id1=$1&id2=$2
    
    比如:http://www.xxx.com/catm6/3/ -> http://www.xxx.com/catm.php?id1=6&id2=3



    希望对大家有所帮助!

  • 相关阅读:
    树莓派GPIO点亮第一个led
    hexo博客域名重复提交问题
    python与arduino串口通讯对接opencv实现智能物品分拣
    python生成excel文件
    python的机器学习之路
    团队冲刺第三天
    团队冲刺第二天
    团队冲刺第一天
    第二阶段任务认领
    构建之法3
  • 原文地址:https://www.cnblogs.com/johnhery/p/9831611.html
Copyright © 2020-2023  润新知