欧美精品免费观看二区/在线观看av/粉嫩国产精品14xxxxx/亚洲精品视频在线观看免费

偽靜態規則寫法RewriteRule-htaccess詳細語法使用
發布時間:2023-10-07

    一、正則表達式教程

    偽靜態規則寫法RewriteRule-htaccess詳細語法使用教程分享

    簡單說下:偽靜態實際上是利用PHP把當前地址解析成另外一種方法進行訪問網站!要學偽靜態規則的寫法你必須得懂一點正則不會沒關系照著下面的套就行

    一、正則表達式教程

    有一個經典的教程: 正則表達式30分鐘入門教程
    這個教程的確很簡單,看完基本上寫一些簡單的正則就沒有問題了。正則是一個需要長期使用的工具,隔段時間不用會忘記,所以我每次都看一遍這個教程。其實學過之后重要的就是一點內容。

    簡單羅列如下:

    .換行符以外的所有字符
    w 匹配字母或數字或下劃線或漢字
    s 匹配任意的空白符
    d 匹配數字
     匹配單詞的開始或結束
    ^ 匹配字符串的開始
    $ 匹配字符串的結束
    * 重復零次或更多次
    + 重復一次或更多次
    ? 重復零次或一次
    {n} 重復n次
    {n}重復n次或更多次
    {nm} 重復n到m次

    應用替換時,前面第一個()中匹配的內容后面就用$1引用,第二個()中匹配的就用$2應用……
    這個個()里面的東東叫原子組
    分析一下 discuz搜索引擎優化 htaccess 里面的重寫。

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


    首先加入用戶通過 kucom.org/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.kucom.org/upgrade.html [R=302L]


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


    三、常用示例

    RewriteEngine On
    RewriteRule index.html index.php


    比如:http://www.kucom.org/index.html  -> http://www.kucom.org/index.php
    RewriteRule ^test([0-9]*).html$ test.php?id=$1

    比如:http://www.kucom.org/test8.html  -> http://www.kucom.org/test.php?id=8
    RewriteRule ^cat-([0-9]+)-([0-9]+).html$ cat.php?id1=$1&id2=$2

    比如:http://www.kucom.org/cat-1-3.html -> http://www.kucom.org/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.kucom.org/cat-zbc2ac-3-5.html -> http://www.kucom.org/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.kucom.org/cat1-4-3-8.html -> http://www.kucom.org/cat1.php?id1=4&id2=3&id3=8
    RewriteRule ^cat([0-9]*)/$ cat.php?id1=$1

    比如:http://www.kucom.org/cat5/ -> http://www.kucom.org/cat.php?id1=5
    RewriteRule ^catm([0-9]*)/([0-9]*)/$ catm.php?id1=$1&id2=$2

    比如:http://www.kucom.org/catm6/3/ -> http://www.kucom.org/catm.php?id1=6&id2=3

    希望對大家有所幫助!