ThinkPHP重寫規則優化URL及Rewrite規則詳細說明

示例以下:php

http://www.topstack.cn/Article/detail/id/198.html 優化爲 http://www.topstack.cn/article-198.htmlhtml

http://www.topstack.cn/Article/index/pid/3/cid/14.html 優化爲 http://www.topstack.cn/index-3-14.htmlnginx

Apache中的rewrite規則以下:api

 

# Apache的rewrite重寫規則,使用前請開啓rewrite模塊


RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^article-(.*)$ index.php/Home/Article/detail/id/$1 [L,NC]
RewriteRule ^index-(.*)-(.*).html$ index.php/Home/Article/index/pid/$1/cid/$2 [L,NC]
RewriteRule ^index-(.*).html$ index.php/Home/Article/index/cid/$1 [L,NC]
RewriteRule ^page-(.*).html$ index.php/Home/Article/page/cid/$1 [L,NC]
RewriteRule ^Article/(.*)$ index.php/Article/$1 [L,NC]
RewriteRule ^Admin$ index.php?m=Admin&c=Public&a=index [L,NC]

 


--------------------------------------------------------------------------------------------------------------
Nginx中的rewrite規則以下:
# Nginxrewrite重寫規則,使用前請開啓rewrite模塊 # 使用方法:將此文件上傳至nginxConf文件夾下,在nginx配置文件的server段中引入此文件便可 # include rewrite.conf
location / {
    if (!-e $request_filename) {
        rewrite ^/article-(.*).html$ /index.php?m=Home&c=Article&a=detail&id=$1 last;
        rewrite ^/index-(.*)-(.*).html$ /index.php?m=Home&c=Article&a=index&pid=$1&cid=$2 last;
        rewrite ^/index-(.*).html$ /index.php?m=Home&c=Article&a=index&cid=$1 last;
        rewrite ^/page-(.*).html$ /index.php?m=Home&c=Article&a=page&cid=$1 last;
        rewrite ^(.*)$ /index.php?s=/$1 last;
        break;
    }
}

 


---------------------------------------------------------------------------------------------------------------

Rewrite是一種服務器的重寫脈衝技術,它能夠使得服務器能夠支持 URL 重寫,是一種最新流行的服務器技術。它還能夠實現限制特定IP訪問網站的功能。服務器


Rewrite標誌 app

R[=code](force redirect) 強制外部重定向
G(force URL to be gone) 強制URL爲GONE,返回410HTTP狀態碼。
P(force proxy) 強制使用代理轉發。
L(last rule) 代表當前規則是最後一條規則,中止分析之後規則的重寫。
N(next round) 從新從第一條規則開始運行重寫過程。
C(chained with next rule) 與下一條規則關聯
若是規則匹配則正常處理,該標誌無效,若是不匹配,那麼下面全部關聯的規則都跳過
T=MIME-type(force MIME type) 強制MIME類型
NS (used only if no internal sub-request) 只用於不是內部子請求
NC(no case) 不區分大小寫
QSA(query string append) 追加請求字符串
NE(no URI escaping of output) 不在輸出轉義特殊字符
例如:
RewriteRule /foo/(.*) /bar?arg=P1%3d$1 [R,NE] 將能正確的將/foo/zoo轉換成/bar?arg=P1=zed
PT(pass through to next handler) 傳遞給下一個處理
例如:
RewriteRule ^/abc(.*) /def$1 [PT] # 將會交給/def規則處理
Alias /def /ghi
S=num(skip next rule(s)) 跳過num條規則
E=VAR:VAL(set environment variable) 設置環境變量

 


----------------------------------------------------------------------------------------------

RewriteCond標誌符優化

'nocase|NC'(no case)忽略大小
'ornext|OR' (or next condition)邏輯或,能夠同時匹配多個RewriteCond條件RewriteRule適用的標誌符
'redirect|R [=code]' (force redirect)強迫重寫爲基於http開頭的外部轉向(注意URL的變化) 如:[R=301,L]
'forbidden|F' (force URL to be forbidden)重寫爲禁止訪問
'proxy|P' (force proxy)重寫爲經過代理訪問的http路徑
'last|L' (last rule)最後的重寫規則標誌,若是匹配,再也不執行之後的規則
'next|N' (next round)循環同一個規則,直到不能知足匹配
'chain|C' (chained with next rule)若是匹配該規則,則繼續下面的有Chain標誌的規則。
'type|T=MIME-type' (force MIME type)指定MIME類型
'nosubreq|NS' (used only if no internal sub-request)若是是內部子請求則跳過
'nocase|NC' (no case)忽略大小
'qsappend|QSA' (query string append)附加查詢字符串
'noescape|NE' (no URI escaping of output)禁止URL中的字符自動轉義成%[0-9]+的形式。
'passthrough|PT' (pass through to next handler)將重寫結果運用於mod_alias
'skip|S=num' (skip next rule(s))跳過下面幾個規則
'env|E=VAR:VAL' (set environment variable)添加環境變量

 


-----------------------------------------------------------------------------------------------------

Rewrite時服務器變量:網站

HTTP headers:HTTP_USER_AGENT, HTTP_REFERER, HTTP_COOKIE, HTTP_HOST, HTTP_ACCEPT
connection & request: REMOTE_ADDR, QUERY_STRING
server internals: DOCUMENT_ROOT, SERVER_PORT, SERVER_PROTOCOL
system stuff: TIME_YEAR, TIME_MON, TIME_DAY

 

 
----------------------------------------------------------------------------------------------------

Rewrite規則表達式的說明:spa

. 匹配任何單字符
[chars] 匹配字符串:chars
[^chars] 不匹配字符串:chars
text1|text2 可選擇的字符串:text1或text2
? 匹配0到1個字符
* 匹配0到多個字符
+ 匹配1到多個字符
^ 字符串開始標誌
$ 字符串結束標誌
n 轉義符標誌
反向引用 $N 用於 RewriteRule 中匹配的變量調用(0 <= N <= 9)
反向引用 %N 用於 RewriteCond 中最後一個匹配的變量調用(1 <= N <= 9)
相關文章
相關標籤/搜索