Apache的配置文件.htaccess是個(gè)難點(diǎn),也是個(gè)重點(diǎn)。一直都沒安下心來好好學(xué)學(xué),甚至一直以為Redirect方法是屬于mod_rewrite,用到的時(shí)候也都是糊里糊涂的。今天找到了一個(gè)很好的教程,一邊學(xué),一邊寫點(diǎn)心得。
updated: 不斷修正一些小問題
Source: Comprehensive guide to .htaccess
Officical reference: Directive Quick Reference
1 Introduction 介紹
文件名 .htaccess 屬性 644 (RW-R–R–)
htaccess會影響它所在目錄下的所有子目錄
注意大多數(shù)內(nèi)容都要求保持在一行之內(nèi),不要換行,否則會引起錯(cuò)誤
2 Error Documents 錯(cuò)誤文檔
Official document: ErrorDocument Directive
ErrorDocument code document
例子
ErrorDocument 400 /errors/badrequest.html
ErrorDocument 404 http://yoursite/errors/notfound.html
ErrorDocument 401 “Authorization Required”
(注意之后內(nèi)容如果出現(xiàn)的雙引號需要轉(zhuǎn)義為 \”)
常見HTTP狀態(tài)碼
Successful Client Requests
200 OK
201 Created
202 Accepted
203 Non-Authorative Information
204 No Content
205 Reset Content
206 Partial Content
Client Request Redirected
300 Multiple Choices
301 Moved Permanently
302 Moved Temporarily
303 See Other
304 Not Modified
305 Use Proxy
Client Request Errors
400 Bad Request
401 Authorization Required
402 Payment Required (not used yet)
403 Forbidden
404 Not Found
405 Method Not Allowed
406 Not Acceptable (encoding)
407 Proxy Authentication Required
408 Request Timed Out
409 Conflicting Request
410 Gone
411 Content Length Required
412 Precondition Failed
413 Request Entity Too Long
414 Request URI Too Long
415 Unsupported Media Type
Server Errors
500 Internal Server Error
501 Not Implemented
502 Bad Gateway
503 Service Unavailable
504 Gateway Timeout
505 HTTP Version Not Supported
3 Password Protection 密碼保護(hù)
Official document: Authentication, Authorization and Access Control
假設(shè)密碼文件為.htpasswd
AuthUserFile /usr/local/safedir/.htpasswd (這里必須使用全路徑名)
AuthName EnterPassword
AuthType Basic
兩種常見驗(yàn)證方式:
Require user windix
(僅允許用戶windix登陸)
Require valid-user
(所有合法用戶都可登陸)
Tip: 如何生成密碼文件
使用htpasswd命令(apache自帶)
第一次生成需要創(chuàng)建密碼文件
htpasswd -c .htpasswd user1
之后增加新用戶
htpasswd .htpasswd user2
4 Enabling SSI Via htaccess 通過htaccess允許SSI(Server Side Including)功能
AddType text/html .shtml
AddHandler server-parsed .shtml
Options Indexes FollowSymLinks Includes
DirectoryIndex index.shtml index.html
5 Blocking users by IP 根據(jù)IP阻止用戶訪問
order allow,deny
deny from 123.45.6.7
deny from 12.34.5. (整個(gè)C類地址)
allow from all
6 Blocking users/sites by referrer 根據(jù)referrer阻止用戶/站點(diǎn)訪問
需要mod_rewrite模塊
例1. 阻止單一referrer: badsite.com
RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} badsite\.com [NC]
RewriteRule .* - [F]
例2. 阻止多個(gè)referrer: badsite1.com, badsite2.com
RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} badsite1\.com [NC,OR]
RewriteCond %{HTTP_REFERER} badsite2\.com
RewriteRule .* - [F]
[NC] - 大小寫不敏感(Case-insensite)
[F] - 403 Forbidden
注意以上代碼注釋掉了”Options +FollowSymlinks”這個(gè)語句。如果服務(wù)器未在 httpd.conf 的 段落設(shè)置 FollowSymLinks, 則需要加上這句,否則會得到”500 Internal Server error”錯(cuò)誤。
7 Blocking bad bots and site rippers (aka offline browsers) 阻止壞爬蟲和離線瀏覽器
需要mod_rewrite模塊
壞爬蟲? 比如一些抓垃圾email地址的爬蟲和不遵守robots.txt的爬蟲(如baidu?)
可以根據(jù) HTTP_USER_AGENT 來判斷它們
(但是還有更無恥的如”中搜 zhongsou.com”之流把自己的agent設(shè)置為 “Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)” 太流氓了,就無能為力了)
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^BlackWidow [OR]
RewriteCond %{HTTP_USER_AGENT} ^Bot\ mailto:craftbot@yahoo.com [OR]
RewriteCond %{HTTP_USER_AGENT} ^ChinaClaw [OR]
RewriteCond %{HTTP_USER_AGENT} ^Custo [OR]
RewriteCond %{HTTP_USER_AGENT} ^DISCo [OR] 本文出自:億恩科技【mszdt.com】
服務(wù)器租用/服務(wù)器托管中國五強(qiáng)!虛擬主機(jī)域名注冊頂級提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM]
|