簡單記錄一下
標準單一redirect
Redirect /page.php http://www.domain.com/destination.php整個資料夾Match
RedirectMatch ^/folder/(.*)$ http://www.domain.com/newfolder/$1Redirect 跟 RedirectMatch 不可以指定query string,以下無法執行
edirect /page.php?id=3 http://mydomain.site/page/3 Redirect /page.php?id=4 http://mydomain.site/page/4 RedirectMatch ^/page.php?id=([0-9]*)$ http://mydomain.site/page/$1要達到以上須使用 modrewrite
RewriteEngine On RewriteCond %{REQUEST_URI} ^/page\.php$ RewriteCond %{QUERY_STRING} ^id=([0-9]*)$ RewriteRule ^(.*)$ http://mydomain.site/page/%1.pdf [R=302,L]以上更簡約的寫法
RewriteEngine On RewriteCond %{QUERY_STRING} ^id=([0-9]*)$ RewriteRule ^page\.php$ http://mydomain.site/page/%1.pdf [R=302,L]實際效果如下
/page.php?id=37 -> http://mydomain.site/page/37.php /page.php?id=40 -> http://mydomain.site/page/40.php
RewriteCond %{HTTP_HOST} ^sub\.oldexample\.com$ [NC]
RewriteCond %{QUERY_STRING} (^|&)value=ABC123(&|$) [NC] RewriteRule ^folder1/page1/?$ http://newexample.com/folder3/page6 [L,R=301,NC]
RewriteCond的語法如下:
RewriteCond TestString CondPattern [Flags]
======================================================
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/5.0.*
RewriteRule index.php index.m.php
RewriteCond %{HTTP_USER_AGENT} ^Lynx.*
RewriteRule index.php index.L.php
RewriteRule index.php index.b.php
上面語句的作用是當你是用FF瀏覽器訪問index.php這個文件的時候,會自動讓你訪問到index.m.php這個文件,當你是用一些移動終端訪問的時候,會讓你對index.php這個文件的訪問實際訪問的是index.L.php去,如果你是用其它的瀏覽器訪問的時候,會讓你跳到index.b.php。在說形像一點,上面的語句就等同於程序裡面的下面語句(依PHP語句為例):
if($_SERVER['HTTP_USER_AGENT'] == 'Mozilla/5.0′)
{
//跳轉到對index.m.php的訪問
}
else if($_SERVER['HTTP_USER_AGENT'] == 'Lynx')
{
//跳轉到對index.L.php的訪問
}
else
//跳轉到對index.b.php的訪問
====================================
再看例2:
RewriteCond %{HTTP_REFERER} (www.test.cn)
RewriteRule (.*)$ test.php
上面語句的作用是如果你訪問的上一個頁面的主機地址是www.test.cn,則無論你當前訪問的是哪個頁面,都會跳轉到對test.php的訪問。
再看例3:
RewriteCond %{REMOTE_HOST} ^host1.* [OR]
RewriteCond %{REMOTE_HOST} ^host2.* [OR]
RewriteCond %{REMOTE_HOST} ^host3.*
RewriteRule (.*)$ test.php
上面語句的作用是如果你的地址是host1或host2或host3的時候,則就跳到對test.php。
從這裡可以看出,RewriteCond語句之間默認的是AND,如果想要OR,則要明確的寫出來。
================================
RewriteCond %{REQUEST_FILENAME} !-f //如果文件不存在,進行下面的RewriteRule.(不是文件或文件不存在就執行重寫)
RewriteCond %{REQUEST_FILENAME} !-d //#如果目錄不存在就進行RewriteRule
RewriteCond %{REQUEST_URI} !^.*(.css|.js|.gif|.png|.jpg|.jpeg)$ //#如果不是該文件進行Rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? /404.php [L]RewriteRule ^/?(.*)$ /404.php?url=$1 [L]RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?([a-zA-Z0-9]+)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?([a-zA-Z0-9]+)$ $1.html [L]RewriteCond %{HTTPS} !on RewriteCond %{SERVER_PORT} !^443$ In situations where secure and unsecured domains share the web server’s DocumentRoot directory, you’ll need a RewriteCond statement to check that the secure server port isn’t being used, and then only redirect the request if the requested script is one in the list of those that require a secure server:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/?(page1|page2|page3|page4|page5)$ https://www.example.com/%1 [R=301,L]Here’s how you’d redirect requests for pages not requiring a secure server back to port 80:
RewriteCond %{ SERVER_PORT } ^443$
RewriteRule !^/?(page6|page7|page8|page9)$ http://www.example.com%{REQUEST_URI} [R=301,L]================================================================
mod_rewrite Flags
常用的R表示redirect(強制重定向),F表示forbidden(禁止訪問),L表示last(最後),通常當你希望停止重寫操作而立即重定向時
NC,表示忽略大小寫
I (ignore case)不管大小寫強行指定字符匹配,這個FLAG影響RewriteRule指令和相應的RewriteCond 指令
F (Forbidden)對客戶端做反應,停止REWRITING進程並且發送403錯誤,注意在這種情況下FORMATSTRING 是無用的並可以設置為任何非空字符串。
L (last rule)不應用任何重寫規則在此停止重寫進程,使用這個FLAG以阻止當前被重寫的URI被後面的規則再次重寫
N (Next iteration)強制REWRITINGENGINE調整規則目標並且從頭重啟規則檢查(所有修改將保存),重啟次數由RepeatLimit指定的值限制,如果這個數值超過N FLAG將被忽略
NS (Next iteration of the same rule)以N標記工作不從相同的規則重啟規則規則進程(例如強制重複規則應用),通過RepeatLimit指令指定一個反复實行某一規則的最大數目,
P (force proxy)強制目的URI在內部強制為代理需求並且立即通過ISAPI擴展應付代理需求,必須確認代理字符串是一個有效的URI包括協議主機等等否則代理將返回錯誤
R (explicit redirect)強制服務器對客戶端發出重定向指示即時應答,提供目的URI的新地址,重定向規則經常是最後規則
QSA – The Query String Appended flag is used to "pass through" existing query strings. You can also define a new query sting to which the old string will be appended, just be careful not to replicate key names. Failure to use the QSA flag will cause the creation of a query string during a redirection to destroy an existing query string.
RP (permanent redirect)幾乎和[R]標記相同但是發布301HTTP狀態而不是302HTTP狀態代碼
U (Unmangle Log)當URI是源需求而不是重寫需求時記載URI
O (nOrmalize)在實行之前標準化字符串。標準化包括URL-ENCODING,不合法的字符的再移動等,這個標記對於URLS和URLS-ENDODED頭是有用的
CL (Case Lower)小寫
CU (Case Upper)大寫
\s匹配任意的空白符,包括空格,製表符(Tab),換行符,中文全角空格等。
\w匹配字母或數字或下劃線或漢字
[ ] 匹配沒有預定義元字符的字符集合,在方括號裡列出它們就行了
例: [aeiou]就匹配任何一個英文元音字母,[.?!]匹配標點符號(.或?或!)
[0-9]代表的含意與\d就是完全一致的:一位數字;同理[a-z0-9A-Z_]也完全等同於\w
.匹配除 “\n” 之外的任何單個字符。要匹配包括'\n' 在內的任何字符,請使用象'[.\n]' 的模式。
例子:\(?0\d{2}[) -]?\d{8}。
* 重複零次或更多次
+ 重複一次或更多次
? 重複零次或一次
{n} 重複n次
{n,} 重複n次或更多次
{n,m} 重複n到m次
======================================================
防止盜鏈,如果來得要訪問jpe jpg bmp png結尾的url 用戶不是來自我們的網站,那麼讓他看一張我們網站的展示圖片。
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https?://(.+.)?mysite.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]
#RewriteRule .*.(gif|jpg|png)$ http://domainname.com/img/hotlink.png[nc]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?example.com/ [NC]
RewriteRule .(gif|jpg|png)$ - [F]網站升級的時候,只有特定IP才能訪問,其他的用戶將看到一個升級頁面
RewriteEngine on
RewriteCond %{REQUEST_URI} !/upgrade.html$
RewriteCond %{REMOTE_HOST} !^24\.121\.202\.30
RewriteRule $ http://www.linuxidc.com/upgrade.html [R=302,L]
把老的域名轉向新域名
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
RewriteRule ^test([0-9]*).html$ test.php?id=$1
:http://www.3code.cn/test8.html -> http://www.3code.cn/test.php?id=8