IT貓撲網(wǎng):您身邊最放心的安全下載站! 最新更新|軟件分類|軟件專題|手機(jī)版|論壇轉(zhuǎn)貼|軟件發(fā)布

您當(dāng)前所在位置: 首頁操作系統(tǒng)LINUX → Unix和linux服務(wù)器上.htaccess文件詳解

Unix和linux服務(wù)器上.htaccess文件詳解

時(shí)間:2015-06-28 00:00:00 來源:IT貓撲網(wǎng) 作者:網(wǎng)管聯(lián)盟 我要評(píng)論(0)

  htaccess可以做大量范圍的事情,包括:文件夾密碼保護(hù)、用戶自動(dòng)重新指向、自定義錯(cuò)誤頁面、變更你的文件擴(kuò)展名、屏蔽特定的用戶IP地址、只允許特定的IP地址、停止目錄表以及使用其他文件作為index文件,等等……

  1. Introduction 介紹

  文件名 .htaccess 屬性 644 (RW-R–R–)

  htaccess會(huì)影響它所在目錄下的所有子目錄

  注意大多數(shù)內(nèi)容都要求保持在一行之內(nèi),不要換行,否則會(huì)引起錯(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)的雙引號(hào)需要轉(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自帶)

  第一次生成需要?jiǎng)?chuàng)建密碼文件

  htpasswd -c .htpasswd user1

  之后增加新用戶

  htpasswd .htpasswd user2

#p#副標(biāo)題#e#

  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, 則需要加上這句,否則會(huì)得到"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:[email protected] [OR]

  RewriteCond %{HTTP_USER_AGENT} ^ChinaClaw [OR]

  RewriteCond %{HTTP_USER_AGENT} ^Custo [OR]

  RewriteCond %{HTTP_USER_AGENT} ^DISCo [OR]

  RewriteCond %{HTTP_USER_AGENT} ^Download\ Demon [OR]

  RewriteCond %{HTTP_USER_AGENT} ^eCatch [OR]

  RewriteCond %{HTTP_USER_AGENT} ^EirGrabber [OR]

  RewriteCond %{HTTP_USER_AGENT} ^EmailSiphon [OR]

  RewriteCond %{HTTP_USER_AGENT} ^EmailWolf [OR]

  RewriteCond %{HTTP_USER_AGENT} ^Express\ WebPictures [OR]

  RewriteCond %{HTTP_USER_AGENT} ^ExtractorPro [OR]

  RewriteCond %{HTTP_USER_AGENT} ^EyeNetIE [OR]

  RewriteCond %{HTTP_USER_AGENT} ^FlashGet [OR]

  RewriteCond %{HTTP_USER_AGENT} ^GetRight [OR]

  RewriteCond %{HTTP_USER_AGENT} ^GetWeb! [OR]

  RewriteCond %{HTTP_USER_AGENT} ^Go!Zilla [OR]

  RewriteCond %{HTTP_USER_AGENT} ^Go-Ahead-Got-It [OR]

  RewriteCond %{HTTP_USER_AGENT} ^GrabNet [OR]

  RewriteCond %{HTTP_USER_AGENT} ^Grafula [OR]

  RewriteCond %{HTTP_USER_AGENT} ^HMView [OR]

  RewriteCond %{HTTP_USER_AGENT} HTTrack [NC,OR]

  RewriteCond %{HTTP_USER_AGENT} ^Image\ Stripper [OR]

  RewriteCond %{HTTP_USER_AGENT} ^Image\ Sucker [OR]

  RewriteCond %{HTTP_USER_AGENT} Indy\ Library [NC,OR]

  RewriteCond %{HTTP_USER_AGENT} ^InterGET [OR]

  RewriteCond %{HTTP_USER_AGENT} ^Internet\ Ninja [OR]

  RewriteCond %{HTTP_USER_AGENT} ^JetCar [OR]

  RewriteCond %{HTTP_USER_AGENT} ^JOC\ Web\ Spider [OR]

  RewriteCond %{HTTP_USER_AGENT} ^larbin [OR]

  RewriteCond %{HTTP_USER_AGENT} ^LeechFTP [OR]

  RewriteCond %{HTTP_USER_AGENT} ^Mass\ Downloader [OR]

  RewriteCond %{HTTP_USER_AGENT} ^MIDown\ tool [OR]

  RewriteCond %{HTTP_USER_AGENT} ^Mister\ PiX [OR]

  RewriteCond %{HTTP_USER_AGENT} ^Navroad [OR]

  RewriteCond %{HTTP_USER_AGENT} ^NearSite [OR]

  RewriteCond %{HTTP_USER_AGENT} ^NetAnts [OR]

  RewriteCond %{HTTP_USER_AGENT} ^NetSpider [OR]

  RewriteCond %{HTTP_USER_AGENT} ^Net\ Vampire [OR]

  RewriteCond %{HTTP_USER_AGENT} ^NetZIP [OR]

  RewriteCond %{HTTP_USER_AGENT} ^Octopus [OR]

  RewriteCond %{HTTP_USER_AGENT} ^Offline\ Explorer [OR]

  RewriteCond %{HTTP_USER_AGENT} ^Offline\ Navigator [OR]

  RewriteCond %{HTTP_USER_AGENT} ^PageGrabber [OR]

  RewriteCond %{HTTP_USER_AGENT} ^Papa\ Foto [OR]

  RewriteCond %{HTTP_USER_AGENT} ^pavuk [OR]

  RewriteCond %{HTTP_USER_AGENT} ^pcBrowser [OR]

  RewriteCond %{HTTP_USER_AGENT} ^RealDownload [OR]

#p#副標(biāo)題#e#

  RewriteCond %{HTTP_USER_AGENT} ^ReGet [OR]

  RewriteCond %{HTTP_USER_AGENT} ^SiteSnagger [OR]

  RewriteCond %{HTTP_USER_AGENT} ^SmartDownload [OR]

  RewriteCond %{HTTP_USER_AGENT} ^SuperBot [OR]

  RewriteCond %{HTTP_USER_AGENT} ^SuperHTTP [OR]

  RewriteCond %{HTTP_USER_AGENT} ^Surfbot [OR]

關(guān)鍵詞標(biāo)簽:Unix,linux,服務(wù)器

相關(guān)閱讀

文章評(píng)論
發(fā)表評(píng)論

熱門文章 安裝紅帽子RedHat Linux9.0操作系統(tǒng)教程 安裝紅帽子RedHat Linux9.0操作系統(tǒng)教程 Tomcat9.0如何安裝_Tomcat9.0環(huán)境變量配置方法 Tomcat9.0如何安裝_Tomcat9.0環(huán)境變量配置方法 多種操作系統(tǒng)NTP客戶端配置 多種操作系統(tǒng)NTP客戶端配置 Linux操作系統(tǒng)修改IP Linux操作系統(tǒng)修改IP

相關(guān)下載

    人氣排行 Linux下獲取CPUID、硬盤序列號(hào)與MAC地址 dmidecode命令查看內(nèi)存型號(hào) linux tc實(shí)現(xiàn)ip流量限制 安裝紅帽子RedHat Linux9.0操作系統(tǒng)教程 linux下解壓rar文件 lcx.exe、nc.exe、sc.exe入侵中的使用方法 Ubuntu linux 關(guān)機(jī)、重啟、注銷 命令 查看linux服務(wù)器硬盤IO讀寫負(fù)載