php 僞靜態詳解

# 僞靜態規格
/**
 * RewriteCond # 判斷
 * RewirteRule # 匹配規則
 * RewirteBase # 跟目錄
 * RewirteMap  # 地址映射地圖,
 */

# rewrite 日誌功能
# apache2.4 以上版本的 LogLevel 
# 設置 LogLevel alert rewrite:trace8 (1~8)
# 查看 apace_error.log 日誌文件
# 只能在conf文件中設置,不能在.htaccess

/***************************************************************************************/
# RewriteRule 語法
# RewriteRule 模式匹配 替換的URL [flags]
# 模式匹配支持Perl 格式 的正則表達式。和 rewrite 的變量
# 替換的url 支持模式匹配的結果和 rewrite變量
# 多個flag 用逗號隔開[R=302,C]

# RewriteRule ^(.*).htm$ /$1.html

# R flag 說明
# 強制外部重定向,後面可以加301,302 跳轉 不加默認302   301 臨時重定向 302 永久重定向
# 例: RewriteRule ^/?(.*)\.html$ /src/$1.php [R=302]
# 區別於內部重定向,瀏覽器上的地址會改變

# RewriteRule ^(.*).htm$ /$1.html [R=301]

# C falg 說明
# 鏈接下一個規則
# RewriteRule ^/?(.*)\.html$ /src/$1.htm [C]
# RewriteRule ^/?(.*)\.htm$ /src/$1.php 
# 與下一條規則成爲一個整體,如果第一條不匹配,下一條就不進入了

# L flag 說明
# 結尾規則,立即停止重寫操作,並不再應用其他重寫規則
# RewriteRule ^(.*) first.php?req=$1 [L]
# RewriteRule ^(.*) second.php?req=$1

# NE flag 說明
# 不對 URL 中特殊字符進行hexcode 轉碼
# RewriteRule ^(.*)\.htm /index.html#$1 [R,NE]

# NC flag 說明
# 不區分大小寫  本身是大小寫敏感的
# RewriteRule ^test/(.*) src/$1 [NC]

# G flag 說明
# 請求的網頁已經失效了(Gone) Apache 服務器會返回410 
# RewriteRule ^oldurl.*$ - [G]

# QSA flag 說明
# 用於在URL 中截取查詢字符串 即 ? 後面的內容
# RewriteRule ^ per/(.*)$ /pre.php?person_id=$1 [QSA,R]
# 訪問 /per/123.php?name=xiaoming

# RewriteBase URL-path
# 設置了目錄級別重寫的基準url 即跟目錄
# RewriteBase /tmp/
# RewriteRule ^(.*)\.html$ $1.htm [R]


/***************************************************************************************/
# RewriteCond TestString CondPattern [flags]
# RewriteCond 指令定義了一個規則的條件
# 即在一個RewriteRule 指令之前有一個或多個RewriteCond 指令
# testString 說明 
# $1~9
# %{name_of_variable} 直接調取服務器變量
# %1~9 RewriteCond 條件中最後符合的條件中的分組成分

# $1~9 引用緊跟在RewriteCond 後面的RewriteRule中模板中匹配的數據
# RewiteCond $1 "test"
# RewriteRule ^(.*)\.htm /$1.html

# 當 用IP訪問的時候,替換成用域名
# RewriteCond %{HTTP_HOST} "127.0.0.1"
# RewriteRule ^(.*)\.htm http://localhost/$1.html [R]

# RewriteCond %{HTTP_HOST} "127.0.0.(.*)"
# RewriteCond %1 "1"

# CondPattern 說明
# -d 是否是目錄  -f 是否是文件

# flags  
# [NC]  大小寫不敏感
# [OR]  邏輯 或   不加默認是 AND 

/***************************************************************************************/
# RewriteMap MapName MapType:MapSource   在http_conf 文件裏配置
# MapName   : 命名給RewriteRule 調用
# MapTyle   : map 文件的類型有 txt rnd
# MapSource : map 文件路徑

# txt 格式  一一對應
# ${MapName:LookupKey | DefaultValue}
# RewriteMap pages txt:C:/wamp/map.txt
# 文件格式
# test1 pagetest1
# test2 pagetest2
# RewriteRule ^(.*)\.htm ${pages:$1}/$1.html

# rnd 格式 隨機映射
# RewriteMap servers rnd:C:/wamp/rnd.txt
# 文件格式
# url1 s1|s2|s3
# url1 s4|s5
# RewriteRule ^(.*)\.htm ${servers:$1|root}/$1.html

/***************************************************************************************/
# 規則關鍵字說明和實例
^ 表示匹配字符串的開始位置
$ 表示匹配字符串的結束位置
? 表示匹配前面字符串0個或多個字符串

[chars]   匹配裏面一個或者多個字符串
[^chars]  匹配排除裏面字符串的字符串
aaa|bbb|ccc 匹配隨便一個
() 取值 $1~n

 /**************************************************************************************/
 # 文件防盜鏈
 RewriteCond %{HTTP_REFERER} !^$
 RewriteCond %{HTTP_REFERER} !localhost [NC]  # 這裏放你的域名
 RewriteRule \.(gif|jpg|png)$ - [F,NC]

 

 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章