一個很好用的.htaccess在線編輯器,支援多國語言。
http://www.htaccesseditor.com/sc.shtml

.htaccess可以做大量範圍的事情,包括:檔夾密碼保護、用戶自動重新指向、自定義錯誤頁面、變更你的檔副檔名、遮罩特定的用戶IP位址、只允許特定的IP位址、停止目錄表以及使用其他檔作為index檔,等等……
1. Introduction 介紹
檔案名 .htaccess 屬性 644 (RW-R–R–)
htaccess會影響它所在目錄下的所有子目錄
注意大多數內容都要求保持在一行之內,不要換行,否則會引起錯誤
2. Error Documents 錯誤文檔
Official document: ErrorDocument Directive
ErrorDocument code document
例子
ErrorDocument 400 /errors/badrequest.html
ErrorDocument 404 http://yoursite/errors/notfound.html
ErrorDocument 401 “Authorization Required”
(注意之後內容如果出現的雙引號需要轉義為 \”)
常見HTTP狀態碼
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 密碼保護
Official document: Authentication, Authorization and Access Control
假設密碼檔為.htpasswd
AuthUserFile /usr/local/safedir/.htpasswd (這裏必須使用全路徑名)
AuthName EnterPassword
AuthType Basic
兩種常見驗證方式:
Require user windix
(僅允許用戶windix登陸)
Require valid-user
(所有合法用戶都可登陸)
Tip: 如何生成密碼檔
使用htpasswd命令(apache自帶)
第一次生成需要創建密碼檔
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 根據IP阻止用戶訪問
order allow,deny
deny from 123.45.6.7
deny from 12.34.5. (整個C類位址)
allow from all
6. Blocking users/sites by referrer 根據referrer阻止用戶/站點訪問
需要mod_rewrite模組
例1. 阻止單一referrer: badsite.com
RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} badsite\.com [NC]
RewriteRule .* - [F]
例2. 阻止多個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”這個語句。如果伺服器未在 httpd.conf 的 段落設置 FollowSymLinks, 則需要加上這句,否則會得到”500 Internal Server error”錯誤。


7. Blocking bad bots and site rippers (aka offline browsers) 阻止壞爬蟲和離線流覽器
需要mod_rewrite模組
壞爬蟲? 比如一些抓垃圾email地址的爬蟲和不遵守robots.txt的爬蟲(如baidu?)
可以根據 HTTP_USER_AGENT 來判斷它們
(但是還有更無恥的如”中搜 zhongsou.com”之流把自己的agent設置為 “Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)” 太流氓了,就無能為力了)



8. Change your default directory page 改變缺省目錄頁面
DirectoryIndex index.html index.php index.cgi index.pl
9. Redirects 轉向
單個文件
Redirect /old_dir/old_file.html http://yoursite.com/new_dir/new_file.html
整個目錄
Redirect /old_dir http://yoursite.com/new_dir
效果: 如同將目錄移動位置一樣
http://yoursite.com/old_dir -> http://yoursite.com/new_dir
http://yoursite.com/old_dir/dir1/test.html -> http://yoursite.com/new_dir/dir1/test.html
Tip: 使用用戶目錄時Redirect不能轉向的解決方法
當你使用Apache默認的用戶目錄,如 http://mysite.com/~windix,當你想轉向 http://mysite.com/~windix/jump時,你會發現下面這個Redirect不工作:
Redirect /jump http://www.google.com
正確的方法是改成
Redirect /~windix/jump http://www.google.com
(source: .htaccess Redirect in “Sites” not redirecting: why?
)
10. Prevent viewing of .htaccess file 防止.htaccess文件被查看
order allow,deny
deny from all
11. Adding MIME Types 添加 MIME 類型
AddType application/x-shockwave-flash swf
Tips: 設置類型為 application/octet-stream 將提示下載
12. Preventing hot linking of images and other file types 防盜鏈
需要mod_rewrite模組
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www/\.)?mydomain.com/.*$ [NC]
RewriteRule \.(gif|jpg|js|css)$ - [F]
解析:
若 HTTP_REFERER 非空 (來源為其他站點,非直接連接) 並且
若 HTTP_REFERER 非(www.)mydomain.com開頭(忽略大小寫[NC]) (來源非本站)
對於所有含有 .gif/.jpg/.js/.css 結尾的檔給出 403 Forbidden 錯誤[F]
也可指定回應,如下例顯示替換圖片
RewriteRule \.(gif|jpg)$
[R,L]
[R] - 轉向(Redirect)
[L] - 連接(Link)
13. Preventing Directory Listing 防止目錄列表時顯示
IndexIgnore *
IndexIgnore *.jpg *.gif
Tips:
允許目錄列表顯示: Options +Indexes
禁止目錄列表顯示: Options -Indexes
顯示提示資訊: 頁首 文件HEADER, 頁尾 文件README