示例如下:
http://www.topstack.cn/Article/detail/id/198.html 優化為 http://www.topstack.cn/article-198.html
http://www.topstack.cn/Article/index/pid/3/cid/14.html 優化為 http://www.topstack.cn/index-3-14.html
Apache中的rewrite規則如下:
# 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規則如下:
# Nginx的rewrite重寫規則,使用前請開啟rewrite模塊 # 使用方法:將此文件上傳至nginx的Conf文件夾下,在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標志
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規則表達式的說明:
. 匹配任何單字符 [chars] 匹配字符串:chars [^chars] 不匹配字符串:chars text1|text2 可選擇的字符串:text1或text2 ? 匹配0到1個字符 * 匹配0到多個字符 + 匹配1到多個字符 ^ 字符串開始標志 $ 字符串結束標志 n 轉義符標志 反向引用 $N 用於 RewriteRule 中匹配的變量調用(0 <= N <= 9) 反向引用 %N 用於 RewriteCond 中最后一個匹配的變量調用(1 <= N <= 9)