去评论
dz插件网

Discuz!Apache环境伪静态规则高级用法:伪静态重定向 防盗链 限制下载 文件保护等

admin
2021/09/06 22:21:28
Discuz!Apache鐜浼潤鎬佽鍒欓珮绾х敤娉:浼潤鎬侀噸瀹氬悜 闃茬洍閾 闄愬埗涓嬭浇 鏂囦欢淇濇姢绛夛細

301鍏ㄧ珯璺宠浆
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.old\.net$ [NC]
RewriteRule ^(.*)$ http://www.new\.net/$1 [L,R=301,NC]
鍏朵腑 http://涓嶈兘鐪佺暐锛屼笉鐒跺嚭鐜扮粷瀵硅矾寰勫姞鍦ㄨ烦杞噸鍐欏墠闈㈢殑
.htaccess 姝e垯琛ㄨ揪寮
# 浣嶈棣栬〃绀烘敞閲
[F] Forbidden (绂佹):
    鍛戒护鏈嶅姟鍣ㄨ繑鍥403 Forbiden 閿欒缁欑敤鎴锋祻瑙堝櫒

[L] Last rule (鏈鍚庝竴鏉¤鍒)
    鍛婅瘔鏈嶅姟鍣ㄥ湪鏈潯瑙勫垯鎵ц瀹屽悗鍋滄閲嶅啓URL

[N] Next 锛堜笅涓鏉¤鍒欙級
    鍛婅瘔鏈嶅姟鍣ㄧ户缁噸鍐欙紝鎸囧鎵鏈夐噸鍐欐寚浠ら兘鎵ц杩

[G] Gone (涓㈠け)
    鍛戒护鏈嶅姟鍣ㄨ繑鍥410 GONE锛坣o longer exists锛夐敊璇秷鎭

[P] Proxy (浠g悊)
    鍛婅瘔鏈嶅姟鍣ㄩ氳繃mod_proxy妯″潡澶勭悊鐢ㄦ埛璇锋眰

[C] Chain (鎹嗙粦)
    鍛婅瘔鏈嶅姟鍣ㄥ皢褰撳墠鐨勮鍒欏拰鍓嶉潰鐨勮鍒欒繘琛屾崋缁

[R] Redirect(閲嶅畾鍚)
    鍛婅瘔鏈嶅姟鍣ㄥ彂鍑洪噸瀹氬悜娑堟伅锛屼互渚跨敤鎴锋祻瑙堝櫒鍙戝嚭rewitten/modified(閲嶅啓/淇敼) 璇锋眰

[NC] No Case(涓嶅尯鍒嗗ぇ灏忓啓)
    瀵瑰鎴风璇锋眰鐨刄RL涓嶅尯鍒嗗ぇ灏忓啓

[PT] Pass Through(鏀捐)
    璁﹎od_rewrite 妯″潡灏嗛噸鍐欑殑URL浼犲洖缁橝pache鍋氳繘涓姝ュ鐞

[OR] Or(閫昏緫鎴)
    鐢ㄩ昏緫鈥滄垨鈥濆皢涓や釜琛ㄨ揪寮忚繛鎺ュ湪涓璧凤紝濡傛灉缁撴灉涓衡滅湡鈥濓紝鍒欎細搴旂敤鍚庣户鐨勭浉鍏宠瀹

[NE] No Escape (绂佹杞箟)
    鍛戒护鏈嶅姟鍣ㄥ湪杈撳嚭鏃剁鐢ㄨ浆涔夊瓧绗

[NS] No Subrequest(绂佺敤瀛愯姹)
    濡傛灉瀛樺湪鍐呴儴璇锋眰锛屽垯璺宠繃褰撳墠鍛戒护

[QSA] Append Query String(杩藉姞鏌ヨ瀛楃涓)
    鍛戒护鏈嶅姟鍣ㄥ湪URL鏈熬杩藉姞鏌ヨ瀛楃涓

[S=x] Skip(璺宠繃)
    濡傛灉婊¤冻鏌愭寚瀹氭潯浠讹紝鍒欒烦杩囧悗闈㈢X鏉¤鍒

[E = Variable:value](鐜鍙橀噺)
    鍛戒护鏈嶅姟鍣ㄤ氦灏嗗 value璧嬬粰鍙橀噺 variable

[T=MIME-type] Mime Type (MIME 绫诲瀷)
    澹版槑鐩爣璧勬簮鎵灞炵殑MIME绫诲瀷

[]
  鍖归厤涓涓瓧绗﹂泦锛屽[xyz]鍙互鍖归厤x,y鎴栬厇

[^]
   濡傦細[xyz]+ 浼氫互浠讳綍椤哄簭锛屾鏁板尮閰峹,y,z鐨勫嚭鐜

[a-z]
    杩炲瓧绗(-)琛ㄧず鍖归厤浠庡瓧姣峚鍒皕鐨勬墍鏈夊瓧绗︿覆

a{n}
    鎸囧畾瀛楁瘝a鍑虹幇鐨勬鏁颁负N娆★紝婊¤冻璇ユ潯浠舵椂鍖归厤銆傚x{3}浠呬笌xxx鍖归厤

a{n,}
    鎸囧畾瀛楁瘝a鍑虹幇鐨勬鏁拌嚦灏戜负N娆★紝渚嬪X{3}鍙互涓嶺XX鎴栬厁xxxx绛夌骇鍖归厤

a{n,m}
    鎸囧畾a鍑虹幇鐨勬鏁拌嚦灏戜负n鍒癿娆   

()
  鐢ㄤ簬灏嗘鍒欒〃杈惧紡鍒嗙粍锛屾弧瓒崇涓缁勬鍒欒〃杈惧紡鐨勫瓧绗︿覆浼氳瀛樺偍鍦ㄥ彉閲$1涓,浠ユ绫绘帹銆傚鏋滄嫭鍙峰憟鐨勪笉鏄鍒欒〃杈惧紡锛屼緥濡傦紙myname锛?press 灏嗚兘澶熷尮閰嶆湁鎴栬呮病鏈塵yname鍓嶇紑鐨刾ress

^
浣嶄簬琛岄銆傛敞鎰忥細鍜屼腑鎷彿涓殑[^]鎰忎箟涓嶅悓

$
浣嶄簬琛屾湯

?
  渚嬪 world?浼氬尮閰峸orld鎴栬厀orldx,鑰屽 wor(ld)? 浼氬尮閰峸or鎴栬厀orld,鍙堝锛歺?浼氬尮閰嶁滅┖瀛楃鈥濇垨鑰呬竴涓獂

!
  閫昏緫闈炪備緥濡傦細"!myname"灏嗕細鍖归厤闄や簡鈥渕yname鈥濅互澶栫殑鎵鏈夊瓧绗︿覆

.
琛ㄧず浠绘剰瀛楃

-
鍛戒护apache鈥滀笉瑕佲濋噸鍐橴RL锛屼緥濡傦細"xxx.domian.com.* -[F]"

+
鍖归厤鑷冲皯涓涓换鎰忓瓧绗︼紝渚嬪锛欸+ 鍖归厤浠寮澶达紝骞朵笖鍚庨潰鑷冲皯鏈変竴涓瓧绗︾殑瀛楃涓

*
鍖归厤闆朵釜鎴栧涓瓧绗︼紝渚嬪锛氣溿傗濆尮閰嶄换鎰忓瓧绗

|
  閫昏緫鈥滄垨鈥濄備笌[OR]涓嶅悓鐨勬槸锛屽畠鏄尮閰嶅瓧绗︿覆锛屼緥濡(x|y)鍖归厤x鎴栬厃


\
  杞箟瀛楃銆傚彲浠ヨ浆涔夊乏鎷彿(锛屽皷鎷彿^,缇庡厓绗﹀彿 $锛 鎰熷徆鍙 锛佺偣 銆 鏄熷彿 *锛岀閬撶鍙穦绛夛紝

\.
杞箟涓虹偣瀛楃锛堢偣瀛楃鍦ㄦ鍒欒〃杈惧紡涓彲浠ュ尮閰嶄换鎰忓瓧绗︼級

/*
  闆朵釜鎴栧涓瓧绗

.*
  闆朵釜鎴栧涓换鎰忓瓧绗︼紙鍗冲尮閰嶄换鎰忓瓧绗︼紝鍖呮嫭绌哄瓧绗︼級

^$
  鍖归厤鈥滅┖瀛楃鈥濓紝鈥滅┖琛屸

^.*$
    鍖归厤浠绘剰瀛楃涓诧紙浠呴檺涓琛岋級

[^/.]
    鍖归厤鏃㈤潪鈥滄鏂滄潬鈥濅篃涓嶆槸鈥滅偣鈥濈殑浠绘剰瀛楃

[^/.]+
    鍖归厤绗竴涓瓧绗︽棦闈炩滄鏂滄潬鈥濅篃涓嶆槸鈥滅偣鈥濓紝鍚庣户瀛楃鍙互鏄滄鏂滄潬鈥濇垨鑰呪滅偣鈥濆瓧绗︿覆

http ://
    鍖归厤 http ://

^domain.*
    鍖归厤浠"domain"寮濮嬬殑瀛楃涓

^domain\.com$
    浠呭尮閰 "domain.com"

-d
    娴嬭瘯瀛楃涓叉槸鍚﹀凡瀛樺湪鐨勭洰褰  

-f
    娴嬭瘯瀛楃涓叉槸鍚︽槸宸茬粡瀛樺湪鐨勬枃浠

-s
    娴嬭瘯瀛楃涓叉墍鎸囨枃浠舵槸鍚︽湁鈥滈潪闆垛濆

浠ヤ笅涓虹浉鍏充緥瀛愶細
1銆傚皢.htm 椤甸潰鏄犲皠鍒 .php

Options +FollowSymlinks
RewriteEngine On
RewriteRule ^(.*)\.htm$ $1.php [NC]

2銆備复鏃堕噸瀹氬悜(R=302)涓庢案涔呴噸瀹氬悜(R=301)

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.htm$ $.php [R,NC,L]
鈥㈣RewriteRule鑳藉灏.htm闈欐侀〉闈㈤噸瀹氬悜鍒.php鍔ㄦ侀〉闈
鈥㈠鏋滈氳繃.htm杩涘叆锛屾祻瑙堝櫒鍦板潃鏍忎細鑷姩杞负.php锛岃繖涔熸槸閲嶅畾鍚戠殑鏈川
鈥㈠繀椤讳繚璇佹湇鍔″櫒涓婃湁瀵瑰簲鐨.php锛屽惁鍒欎細404
鈥㈡祻瑙堝櫒鍜屾悳绱㈠紩鎿庡彲浠ュ悓鏃堕氳繃.htm鍜.php璁块棶缃戦〉
鈥㈠鏋滆鐩綍涓婂瓨鍦.htm锛屽皢琚拷鐣
鈥ewriteBase瀹氫箟浜嗛噸鍐欏熀鍑嗙洰褰曘
鈥緥濡傦紝濡傛灉浣犲皢铏氭嫙绔欑偣璁剧疆鍦/var/www鐩綍涓嬶紝鍒犻櫎杩欒灏嗕細瀵艰嚧閲嶅畾鍚戝埌h ttp://yourdomain.com/var/www/1.php 銆傛樉鐒惰繖鏄壘涓嶅埌鐨勶紝鑰屼笖浣犱篃涓嶄細甯屾湜鐢ㄦ埛鐪嬭浣犵殑鏈嶅姟鍣ㄧ殑鐩綍缁撴瀯銆
鈥㈠啀涓句釜渚嬪瓙锛屽鏋淩ewriteBase /base/锛岄偅涔堝皢浼氶噸瀹氬悜鍒癶 ttp://yourdomain.com/base/1.php 銆
鈥㈠浜庨噸鍐欏熀鍑嗙洰褰曪紝鎴戜滑杩樺彲浠ラ氳繃灏$1.php鍙樻垚/$1.php瀹炵幇鐩存帴鍙樻崲锛岃繖鏃跺氨鍙互灏哛ewriteBase鐪佺暐銆
鈥㈠瓧姣峈琛ㄧず涓存椂閲嶅畾鍚戯紝鐩稿綋浜嶽R=302,NC]銆傚叧浜庨噸瀹氬悜浠g爜锛岃鍙傝僪 ttp://lesca.me/archives/htaccess-appendix.html 銆奌TTP鍗忚閲嶅畾鍚戠紪鐮併
鈥㈠瓧姣峀琛ㄧず濡傛灉鑳藉尮閰嶆湰鏉¤鍒欙紝閭d箞鏈潯瑙勫垯鏄渶鍚庝竴鏉(Last)锛屽拷鐣ヤ箣鍚庣殑瑙勫垯銆

姘镐箙寰楀畾鍚 R=301
RewriteEngine On
RewriteRule ^(.*)$ h ttp://newdomain.com/$ [R=301,NC,L]
鈥㈣繖涓鍒欏憡璇夋祻瑙堝櫒鍜屾悳绱㈠紩鎿庯紝缃戠珯鍦板潃鍙戠敓浜嗘案涔呮у彉鏇达紝鐢ㄦ埛鐨刄RL璇锋眰灏嗕細琚彂閫佺粰鏂扮殑鍩熷悕锛堜富鏈猴級澶勭悊銆
鈥㈢敱浜庢槸閲嶅畾鍚戝埌鏂扮殑涓绘満鍦板潃锛孯ewriteBase涔熷氨娌℃湁鍑虹幇鐨勫繀瑕佷簡銆

3.涓轰粈涔堣鐢ㄩ噸瀹氬悜锛熲斺旈噸瀹氬悜鍜孶RL閲嶅啓鐨勫尯鍒
鈥㈤氳繃閲嶅畾鍚戯紝娴忚鍣ㄧ煡閬撻〉闈綅缃彂鐢熷彉鍖栵紝浠庤屾敼鍙樺湴鍧鏍忔樉绀虹殑鍦板潃
鈥㈤氳繃閲嶅畾鍚戯紝鎼滅储寮曟搸鎰忚瘑鍒伴〉闈㈣绉诲姩浜嗭紝浠庤屾洿鏂版悳绱㈠紩鎿庣储寮曪紝灏嗗師鏉ュけ鏁堢殑閾炬帴浠庢悳绱㈢粨鏋滀腑绉婚櫎
鈥复鏃堕噸瀹氬悜(R=302)鍜屾案涔呴噸瀹氬悜(R=301)閮芥槸浜叉悳绱㈠紩鎿庣殑锛屾槸SEO鐨勯噸瑕佹妧鏈
鈥RL閲嶅啓鐢ㄤ簬灏嗛〉闈㈡槧灏勫埌鏈珯鍙︿竴椤甸潰锛岃嫢閲嶅啓鍒板彟涓缃戠粶涓绘満锛堝煙鍚嶏級锛屽垯鎸夐噸瀹氬悜澶勭悊

闀跨煭鍦板潃杞崲
鍒╃敤URL閲嶅啓锛屾垜浠彲浠ュ緢鏂逛究鍦板疄鐜伴暱鐭湴鍧鐨勮浆鎹紝浣嗘槸鐢ㄩ噸瀹氬悜灏变笉鍚堥備簡銆

RewriteEngine On
RewriteRule ^grab /public/file/download/download.php

鑻ヨ闂
h ttp://mysite/grab?file=my.zip

鍒欎細鎵ц璇ラ〉闈細
h ttp://mysite/public/files/download/download.php?file=my.zip

鍘绘帀www

options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ h ttp://$1/$1 [R=301,NC,L]

鍔犱笂www

optionEngine On
RewriteCond %{HTTP_HOST} ^(.*)$
RewriteRule (.*) h ttp://www\.%1/$1 [R=301,L]

鏀寔澶氬煙鍚嶈闂
濡傛灉浣犱笉鍑戝阀涔板埌浜嗕笉鏀寔澶氬煙鍚嶇殑涓绘満锛岄偅涔.htaccess鎴栬鍙互甯姪浣犮傜幇鍦ㄥ亣璁句綘鏈夊煙鍚峝omain-one.com鍜宒omain-two.com锛屽苟涓斿湪鏈嶅姟鍣ㄦ牴鐩綍鏈夊搴旀枃浠跺すone鍜宼wo锛岄偅涔堥氳繃涓嬮潰鐨勬敼鍐欏氨鑳借Apache鍚屾椂鎺ュ彈鑰呬袱涓煙鍚嶇殑璇锋眰锛

RewriteCond %{HTTP_HOST} domain-one.com
RewriteCond %{REQUEST_URI} !^/one
RewriteRule ^(.*)$ /one/$1 [L]

RewriteCond %{HTTP_HOST} domain-two.com
RewriteCond %{REQUEST_URL} !^/two
RewriteRule ^(.*)$ /two/$1 [L]

鏀瑰啓鏌ヨ瀛楃涓睶UERY_STRING
鏌ヨ瀛楃涓叉槸鎸嘦RL璇锋眰涓滈棶鍙封濆悗闈㈢殑閮ㄥ垎銆傛瘮濡傦紝h ttp://mysite/grab?foo=bar 锛熼儴鍒嗗氨鏄煡璇㈠瓧绗︿覆锛屽叾涓彉閲忓悕鏄痜oo锛屽兼槸bar銆
1.鍒╃敤QSA杞崲鏌ヨ瀛楃涓睶UERY_STRING
QSA鏍囧織锛 Query String Appending锛夌敤浜庡湪URI涓埅鍙栨煡璇㈠瓧绗︿覆锛岃繖涓埅鍙栨搷浣滄槸閫氳繃灏忔嫭鍙锋鍒欒〃杈惧紡瀹炵幇鐨勶細

RewriteEngine On
RewriteRule /pages/(.+) /page.php?page=$1 [QSA]

鈥㈠皢浼氭妸璇锋眰/pages/123?one=two 鏄犲皠鍒 /page.php?page=123&one=two
鈥㈡敞鎰忓嚑涔庢槸鐩稿悓鐨勶紝闄や簡鈥滈棶鍙封濆彉鎴愪簡鈥滀笌鈥濈鍙
鈥㈠鏋滄病鏈塓SA鏍囧織锛岄偅涔堜細鏄犲皠鍒/page.php?page=123銆
鈥㈠鏋滄病鏈夌敤鍒板皬鎷彿姝e垯琛ㄨ揪寮忥紝灏变笉闇瑕丵SA
鈥㈠皬鎷彿姝e垯琛ㄨ揪寮忓彲浠ユ埅鍙栨煡璇㈠瓧绗︿覆涓殑鍐呭锛屼絾鏄鏋滄病鏈夊紑鍚疩SA鏍囧織锛岄偅涔堝湪/page.php?page=$1涓滈棶鍙封濅箣鍚庡皢浼氳鍓ョ涓㈠純銆傝繖绉嶇壒鎬у彲浠ョ敤浜庡疄鐜扳滃墺绂绘煡璇㈠瓧绗︿覆鈥

閫氳繃QSA锛屾垜浠彲浠ュ皢绠鍗曢摼鎺/simple/flat/link/ 鏄犲皠鎴 server-side.php?first-var=flat&second-var=link

RewriteEngine On
RewriteRule ^/([^/]+)/([^/]+)/? /index.php?first-var=$1&second-var=$2 [QSA]

鍒╃敤RewriteCond鏀瑰啓鏌ヨ瀛楃涓睶UERY_STRING
RewriteEngine On
RewriteCond %{QUERY_STRING} foo=(.*)
RewriteRule ^grab(.*) /page.php?bar=%1

璇ヨ鍒欏皢璁块棶璇锋眰htt p://mysite/grab?foo=bar杞崲涓篽tt p://mysite/page.php?bar=bar
RewriteCond鐢ㄤ簬鎹曡幏鏌ヨ瀛楃涓诧紙QUERY_STRING锛変腑鍙橀噺foo鐨勫硷紝骞跺瓨鍌ㄥ湪%1涓
QUERY_STRING鏄疉pache瀹氫箟鐨勨滃彉閲=鍊尖濆悜閲忥紙鏁扮粍锛

QSA涓嶳ewriteCond涓璧风敤

RewriteEngine On
RewriteCond %{QUERY_STRING} foo=(.+)
RewriteRule ^grab/(.*) /%1/index.php?file=$1 [QSA]

鈥細鎶/grab/foobar.zip?level=5&foo=bar 鏄犲皠鍒 /bar/index.php?file=foobar.zip&level=5&foo=bar
鈥㈣浆鎹㈠悗鏍圭洰褰曟槸bar鐩綍
鈥oobar.zip?level=5涓殑鈥滈棶鍙封濆彉鎴愪簡foobar.zip&level=5涓殑鈥滀笌鈥濈鍙

鍓ョ鏌ヨ瀛楃涓

鍙渶鍦ㄨ寮濮嬪墺绂荤殑閾炬帴鍚庨潰鍔犱釜鈥滈棶鍙封濓紝骞朵笖涓嶈鍚敤QSA鏍囧織锛屽氨鍙墺绂绘煡璇㈠瓧绗︿覆

RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule foo.php(.*) /foo.php? [L]

鐢.htaccess闃绘User-agent
浠涔堟槸User-agent锛烾ser-agent鐢ㄤ簬娴忚鍣ㄥ悜鏈嶅姟鍣ㄢ滆嚜鎶ュ闂ㄢ濓紝鏇寸‘鍒囩殑璇存槸鎵鏈塇TTP瀹㈡埛绔兘寰楃敤User-agent鍚戞湇鍔″櫒鈥滆嚜鎶ュ闂ㄢ濓紝浠ヤ究鏈嶅姟鍣ㄥ涓嶅悓鐨勫鎴风浣滃嚭涓嶅悓鍝嶅簲銆傛瘮濡傦紝鏌愮珯鐐瑰彲鑳介渶瑕佸娴忚鍣ㄣ佹悳绱㈠紩鎿巆rawl杩樻湁鍚勭被涓嬭浇宸ュ叿浣滃嚭涓嶅悓鐨勫搷搴斻傛湇鍔″櫒灏辨槸閫氳繃鎵璋撶殑User-agent杩涜鍖哄垎鐨勩
濡傛灉浣犵殑鏈嶅姟鍣ㄦ彁渚涙煇浜涜祫婧愮殑涓嬭浇锛岄偅涔堜綘灏卞繀椤诲鍔犲皬蹇冭濡傗滆繀闆封濈瓑涓嬭浇杞欢锛屽洜涓哄畠浠彲鑳芥妸浣犵綉绔欒祫婧愬惛骞诧紝骞朵笖褰卞搷浣犵殑姝e父璁垮璁块棶銆備负姝わ紝鎴戜滑鍙互鍒╃敤Rewrite闄愬埗鏌愪簺UA鐨勮闂細

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} 2.0.50727 [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^BlackWido [NC,OR]
# ect...
RewriteRule . abuse.txt


鈥㈣瑙勫垯闄愬埗鈥滆繀闆封濆鎴风涓嬭浇璧勬簮锛屽苟灏嗕笅杞芥枃浠堕噸缃埌abuse.txt
鈥TTP_USER_AGENT鏄疉pache鐨勫唴缃彉閲
鈥2.0.50727鏄繀闆稶ser-agent鐨勭壒寰佸瓧绗︿覆
鈥ewriteRule鍚庨潰鐨勨滅偣鈥濊〃绀衡滀换鎰廢RI鈥濓紝涔熷氨鏄笉绠¤姹傜殑鏄粈涔堬紝閮借緭鍑篴buse.txt

鏂囦欢璁块棶鎺у埗

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^(.+)\.css$
RewriteCond %{REQUEST_FILENAME} !^(.+)\.js$
RewriteCond %{REQUEST_FILENAME} !special.zip$
RewriteRule ^(.+)$ /chat/ [NC]

鈥㈣瑙勫垯灏嗕粎鍏佽鐢ㄦ埛璇锋眰.css, .js绫诲瀷鐨勬枃浠讹紝杩樻湁special.zip鏂囦欢
鈥ewriteRule 鍚庨潰鎸囧畾浜嗛檺鍒惰鍒欙細鏄犲皠鍒/char/鐩綍涓嬪鐞
鈥ewriteCond 鍚庨潰鐨勨滄劅鍙瑰彿鈥(!)璧峰埌浜嗏滃惁瀹氣濅綔鐢紝瀹冭〃鏄庯紝瀵逛笉婊¤冻鍚庨潰姝e垯琛ㄨ揪寮忚呭簲鐢≧ewriteRule瑙勫垯锛屼篃灏辨槸瀵瑰綋鍓嶇被鍨嬬殑鏂囦欢灏嗕笉搴旂敤瑙勫垯
鈥ewriteCond 涔嬮棿鏄互閫昏緫鈥滀笌鈥濊繛鎺ョ殑锛屼篃灏辨槸鍙湁褰撲笁涓潯浠堕兘涓嶆弧瓒虫椂鎵嶆墽琛孯ewriteRule
鈥㈣瑙勫垯涔熶細闄愬埗璁块棶.htm, .jpg绛夋牸寮
鈥㈣瑙勫垯涓嶅彲浠ユ斁鍦ㄨ櫄鎷熺珯鐐规牴鐩綍锛/锛変笅锛屽惁鍒欎細姝诲惊鐜
鈥㈠鏋滄槸浜岀骇鐩綍锛屽/test/锛岄偅涔堜紶鍏ewriteCond鐨勫弬鏁版槸浠/test/寮濮嬬殑锛屽洜姝や粠(.+)鑾峰緱鐨勬枃浠跺悕涔熷惈鏈/test/锛岃鑰呭繀椤诲姝ゅ鍔犲皬蹇
鈥㈣鎯充粎鑾峰緱鏂囦欢鍚嶏紝鍙互灏(.+)鏇挎崲鎴([^/]+)锛屽苟涓斿幓鎺夌鍙穅锛屽涓嬫墍绀猴細

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !([^/]+)\.css$
RewriteCond %{REQUEST_FILENAME} !([^/]+)\.js$
RewriteRule ^(.+)$ /chat/ [NC]




RewriteEngine Off
#RewiteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?xxx\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ blog/$1

# 娌℃湁杈撳叆鏂囦欢鍚嶇殑榛樿鍒板埌棣栭〉
RewriteCond %{HTTP_HOST} ^(www\.)?xxx\.com$ [NC]
RewriteRule ^(/)?$ blog/index.php [L]

闃茬洍閾撅細
RewriteEngine on
#RewiteBase /
RewriteCond %{HTTP_REFERER} !^$ [NC]
RewriteCond %{HTTP_REFERER} !xxx.info [NC]
RewriteCond %{HTTP_REFERER} !(www\.)?baidu.com$ [NC]
RewriteCond %{HTTP_REFERER} !(www\.).google.com$ [NC]
RewriteRule \.(jpg|gif|png|bmp|swf|jpeg)$ /error/daolian.gif [R,NC,L]

銆怰ewriteCond璇硶銆
RewriteCond TestString CondPattern [flags]

rewritecond鐨勫叾浠栫敤娉曪細
"-d"(鐩綍)
灏員estString瑙嗕负涓涓矾寰勫悕骞舵祴璇曞畠鏄惁涓轰竴涓瓨鍦ㄧ殑鐩綍銆
"-f"(甯歌鏂囦欢)
灏員estString瑙嗕负涓涓矾寰勫悕骞舵祴璇曞畠鏄惁涓轰竴涓瓨鍦ㄧ殑甯歌鏂囦欢銆
"-s"(闈炵┖鐨勫父瑙勬枃浠)
灏員estString瑙嗕负涓涓矾寰勫悕骞舵祴璇曞畠鏄惁涓轰竴涓瓨鍦ㄧ殑銆佸昂瀵稿ぇ浜0鐨勫父瑙勬枃浠躲
"-l"(绗﹀彿杩炴帴)
灏員estString瑙嗕负涓涓矾寰勫悕骞舵祴璇曞畠鏄惁涓轰竴涓瓨鍦ㄧ殑绗﹀彿杩炴帴銆
"-x"(鍙墽琛)
灏員estString瑙嗕负涓涓矾寰勫悕骞舵祴璇曞畠鏄惁涓轰竴涓瓨鍦ㄧ殑銆佸叿鏈夊彲鎵ц鏉冮檺鐨勬枃浠躲傝鏉冮檺鐢辨搷浣滅郴缁熸娴嬨
"-F"(瀵瑰瓙璇锋眰瀛樺湪鐨勬枃浠)
妫鏌estString鏄惁涓轰竴涓湁鏁堢殑鏂囦欢锛岃屼笖鍙互鍦ㄦ湇鍔″櫒褰撳墠鐨勮闂帶鍒堕厤缃笅琚闂傚畠浣跨敤涓涓唴閮ㄥ瓙璇锋眰鏉ュ仛妫鏌ワ紝鐢变簬浼氶檷浣庢湇鍔″櫒鐨勬ц兘锛屾墍浠ヨ璋ㄦ厧浣跨敤锛
"-U"(瀵瑰瓙璇锋眰瀛樺湪鐨刄RL)
妫鏌estString鏄惁涓轰竴涓湁鏁堢殑URL锛岃屼笖鍙互鍦ㄦ湇鍔″櫒褰撳墠鐨勮闂帶鍒堕厤缃笅琚闂傚畠浣跨敤涓涓唴閮ㄥ瓙璇锋眰鏉ュ仛妫鏌ワ紝鐢变簬浼氶檷浣庢湇鍔″櫒鐨勬ц兘锛屾墍浠ヨ璋ㄦ厧浣跨敤锛

銆怰ewriteRule璇硶锛氥
RewriteRule Pattern Substitution [flags]

銆恌lags銆
"chain|C"(閾炬帴涓嬩竴瑙勫垯)


"cookie|CO=NAME:VAL:domain[:lifetime[:path]]"(璁剧疆cookie)


"env|E=VAR:VAL"(璁剧疆鐜鍙橀噺)


"forbidden|F"(寮哄埗绂佹URL)


"gone|G"(寮哄埗搴熷純URL)


"handler|H=Content-handler"(寮哄埗鎸囧畾鍐呭澶勭悊鍣)


"last|L"(缁撳熬瑙勫垯)


"next|N"(浠庡ご鍐嶆潵)


"nocase|NC"(蹇界暐澶у皬鍐)


"noescape|NE"(鍦ㄨ緭鍑轰腑涓嶅URI杩涜杞箟)


"nosubreq|NS"(涓嶅鍐呴儴瀛愯姹傝繘琛屽鐞)


"proxy|P"(寮哄埗涓轰唬鐞)


"passthrough|PT"(绉讳氦缁欎笅涓涓鐞嗗櫒)


"qsappend|QSA"(杩藉姞鏌ヨ瀛楃涓)


"redirect|R [=code]"(寮哄埗閲嶅畾鍚)


"skip|S=num"(璺宠繃鍚庣户瑙勫垯)


"type|T=MIME-type"(寮哄埗MIME绫诲瀷)

RewriteRule ^(.+\.php)s$ $1 [T=application/x-httpd-php-source]
绀轰緥锛
RewriteEngine OnRewriteCond %{HTTP_HOST} ^(www\.)?xxx\.com$RewriteCond %{REQUEST_URI} !^/blog/RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ /blog/$1# 娌℃湁杈撳叆鏂囦欢鍚嶇殑榛樿鍒板埌棣栭〉RewriteCond %{HTTP_HOST} ^(www\.)?xxx\.com$RewriteRule ^(/)?$ blog/index.php [L]
銆怰ewriteEngine On銆
琛ㄧず閲嶅啓寮曟搸寮锛屽叧闂璷ff锛屼綔鐢ㄥ氨鏄柟渚跨殑寮鍚垨鍏抽棴浠ヤ笅鐨勮鍙ワ紝杩欐牱灏变笉闇瑕佷竴鏉′竴鏉$殑娉ㄩ噴璇彞浜嗐
銆怰ewriteCond %{HTTP_HOST} ^(www\.)?xxx\.com$銆
杩欐槸閲嶅啓鏉′欢锛屽墠闈%{HTTP_HOST}琛ㄧず褰撳墠璁块棶鐨勭綉鍧锛屽彧鏄寚鍓嶇紑閮ㄥ垎锛屾牸寮忔槸www.xxx.com涓嶅寘鎷渉ttp://鈥濆拰鈥/鈥濓紝^琛ㄧず 瀛楃涓插紑濮嬶紝$琛ㄧず瀛楃涓茬粨灏撅紝\.琛ㄧず杞箟鐨. 锛屽鏋滀笉杞箟涔熻锛屾帹鑽愯浆涔夛紝闃叉鏈変簺鏈嶅姟鍣ㄤ笉鏀寔锛?琛ㄧず鍓嶉潰鎷彿www\.鍑虹幇0娆℃垨1娆★紝杩欏彞瑙勫垯鐨勬剰鎬濆氨鏄鏋滆闂殑缃戝潃鏄痻xx.com鎴栬 www.xxx.com灏辨墽琛屼互涓嬬殑璇彞锛屼笉绗﹀悎灏辫烦杩囥
銆怰ewriteCond %{REQUEST_URI} !^/blog/銆
涔熸槸閲嶅啓鏉′欢锛%{REQUEST_URI}琛ㄧず璁块棶鐨勭浉瀵瑰湴鍧锛屽氨鏄浉瀵规牴鐩綍鐨勫湴鍧锛屽氨鏄煙鍚/鍚庨潰鐨勬垚鍒嗭紝鏍煎紡涓婂寘鎷渶鍓嶉潰鐨勨/鈥濓紝!琛ㄧず闈烇紝杩欏彞璇彞琛ㄧず璁块棶鐨勫湴鍧涓嶄互/blog/寮澶达紝鍙槸寮澶碸锛屾病鏈夌粨灏$
銆怰ewriteCond %{REQUEST_FILENAME} !-f銆
銆怰ewriteCond %{REQUEST_FILENAME} !-d銆
杩欎袱鍙ヨ鍙ョ殑鎰忔濇槸璇锋眰鐨勬枃浠舵垨璺緞鏄笉瀛樺湪鐨勶紝濡傛灉鏂囦欢鎴栬矾寰勫瓨鍦ㄥ皢杩斿洖宸茬粡瀛樺湪鐨勬枃浠舵垨璺緞
銆怰ewriteRule ^(.*)$ /blog/$1銆
閲嶅啓瑙勫垯锛屾渶閲嶈鐨勯儴鍒嗭紝鎰忔濇槸褰撲笂闈㈢殑RewriteCond鏉′欢閮芥弧瓒崇殑鏃跺欙紝灏嗕細鎵ц姝ら噸鍐欒鍒欙紝^(.*)$鏄竴涓鍒欒〃杈剧殑 鍖归厤锛屽尮閰嶇殑鏄綋鍓嶈姹傜殑URL锛宆(.*)$鎰忔濇槸鍖归厤褰撳墠URL浠绘剰瀛楃锛.琛ㄧず浠绘剰鍗曚釜瀛楃锛*琛ㄧず鍖归厤0娆℃垨N娆★紙N>0锛夛紝鍚庨潰 /blog/$1鏄噸鍐欐垚鍒嗭紝鎰忔濇槸灏嗗墠闈㈠尮閰嶇殑瀛楃閲嶅啓鎴/blog/$1锛岃繖涓$1琛ㄧず鍙嶅悜鍖归厤锛屽紩鐢ㄧ殑鏄墠闈㈢涓涓渾鎷彿鐨勬垚鍒嗭紝鍗砠(.*)$涓 鐨.* 锛屽叾瀹炶繖鍎垮皢浼氬嚭鐜颁竴涓棶棰橈紝鍚庨潰璁ㄨ銆

銆怰ewriteCond %{HTTP_HOST} ^(www\.)?xxx\.com$銆
銆怰ewriteRule ^(/)?$ blog/index.php [L]銆
杩欎袱鍙ョ殑鎰忔濇槸鎸囪姹傜殑host鍦板潃鏄痺ww.xxx.com鏄紝濡傛灉鍦板潃鐨勭粨灏惧彧鏈0涓垨鑰1涓/鈥濇椂锛屽皢浼氶噸鍐欏埌瀛愮洰褰曚笅鐨勪富椤碉紝鎴戠寽鎯宠繖涓昏鍥犱负閲嶅啓鍚庣殑鍦板潃鏄笉鑳借嚜鍔ㄥ鎵句富椤电殑锛岄渶瑕佽嚜宸辨寚瀹氥

鐜板湪璇磋鍑虹幇鐨勯棶棰橈紝RewriteRule ^(.*)$ /blog/$1 鍓嶉儴鍒 ^(.*)$ 灏嗕細鍖归厤褰撳墠璇锋眰鐨剈rl銆
渚嬪锛氳姹傜綉鍧鏄痟ttp://www.xxx.com/a.html锛屽埌搴曟槸鍖归厤鏁翠釜http://www.xxx.com/a.html锛岃繕鏄彧鍖归厤/a.html鍗冲弽鏂滄潬鍚庨潰鐨勬垚鍒嗭紝杩樻槸鍙尮閰峚.html銆
绛 妗堟槸锛(鏍规嵁RewriteBase瑙勫垯瑙勫畾锛屽鏋渞ewritebase 涓/锛屽皢浼氬尮閰峚.html锛屼笉甯﹀墠闈㈢殑鍙嶆枩鏉狅紝鎵浠ヤ笂鏉¤鍙ュ簲璇ュ啓鎴怰ewriteRule ^(.*)$ blog/$1锛堜笉甯/锛夛紝涓嶈繃瀹為檯搴旂敤涓婂甫涓婂墠闈㈢殑鍙嶆枩鏉狅紝涔熷彲浠ョ敤锛屽彲鑳藉甫涓嶅甫閮借銆傜幇鍦ㄩ棶棰樺嚭鏉ヤ簡锛屽鏋滀笉璁剧疆rewritebase 涓/ 锛屽皢浼氬尮閰嶆暣涓綉鍧http://www.xxx.com/a.html锛屾樉鐒惰繖鏄敊璇殑锛屾墍浠ュ簲璇ユ坊鍔犺繖鏉★細RewiteBase /)鏈夎锛岃嚜宸辨祴璇曢氫笉杩囷紝鎵浠ョ‘鐨勫簲璇ユ槸 RewriteRule 涓殑璺宠浆閮ㄥ垎鍔犱笂 http://鍗忚

杩樻湁涓涓棶棰樻槸锛屼笉鑳戒繚璇佹瘡涓汉杈撳叆鐨勭綉鍧閮芥槸灏忓啓鐨勶紝濡傛灉杈撳叆澶у啓鐨勫憿锛宭inux绯荤粺鏄尯鍒嗗ぇ灏忓啓鐨勶紝鎵浠ュ簲璇ュ湪RewriteCond鍚庢坊鍔燵NC]蹇界暐澶у皬鍐欑殑銆
鑷虫锛屽畬鏁寸殑璇彞搴旇鏄細

RewriteEngine OnRewiteBase /RewriteCond %{HTTP_HOST} ^(www\.)?xxx\.com$ [NC]RewriteCond %{REQUEST_URI} !^/blog/RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ blog/$1# 娌℃湁杈撳叆鏂囦欢鍚嶇殑榛樿鍒板埌棣栭〉RewriteCond %{HTTP_HOST} ^(www\.)?xxx\.com$ [NC]RewriteRule ^(/)?$ blog/index.php [L]