簡單易用的.htaccess

.htaccess 文件 (Hypertext Accessfile) 是Apache Web服務器的一個非常強大的配置文件,對於這個文件,Apache有一堆參數可以讓你配置出幾乎隨心所欲的功能。.htaccess 配置文件堅持了Unix的一個文化——使用一個ASCII 的純文本文件來配置你的網站的訪問策略。另外,因爲.htaccess 是一個相當強大的配置文件,所以,一個輕微的語法錯誤會造成你整個網站的故障,所以,在你修改或是替換原有的文件時,一定要備份舊的文件,以便出現問題的時候可以方便的恢復。

 

使用.htaccess 創建自定義的出錯頁面。對於Linux Apache來說這是一項極其簡單的事情。使用下面的.htaccess語法你可以輕鬆的完成這一功能。(把.htaccess放在你的網站根目錄下)

ErrorDocument 401 /error/401.php

ErrorDocument 403 /error/403.php

ErrorDocument 404 /error/404.php

ErrorDocument 500 /error/500.php

 

設置網站的時區

SetEnv TZ America/Houston

 

阻止IP列表

有些時候,你需要以IP地址的方式阻止一些訪問。無論是對於一個IP地址還是一個網段,這都是一件非常簡單的事情,如下所示:

allow from all

deny from 145.186.14.122

deny from 124.15

Apache對於被拒絕的IP會返回403錯誤。

 

把一些老的鏈接轉到新的鏈接上——搜索引擎優化SEO

Redirect 301 /d/file.htmlhttp://www.htaccesselite.com/r/file.html

 

阻止 User Agent 的所有請求

## .htaccess Code :: BEGIN

## Block Bad Bots by user-Agent

SetEnvIfNoCase user-Agent ^FrontPage[NC,OR]

SetEnvIfNoCase user-Agent ^Java.* [NC,OR]

SetEnvIfNoCase user-Agent ^Microsoft.URL[NC,OR]

SetEnvIfNoCase user-Agent ^MSFrontPage[NC,OR]

SetEnvIfNoCase user-Agent ^Offline.Explorer[NC,OR]

SetEnvIfNoCase user-Agent ^[Ww]eb[Bb]andit[NC,OR]

SetEnvIfNoCase user-Agent ^Zeus [NC]

 

Order Allow,Deny

Allow from all

Deny from env=bad_bot

 

## .htaccess Code :: END

 

把某些特殊的IP地址的請求重定向到別的站點

ErrorDocument 403 http://www.youdomain.com

Order deny,allow

Deny from all

Allow from ip

Allow from ip

 

直接找開文件而不是下載– 通常,我們打開網上文件的時候總是會出現一個對話框問我們是下載還是直接打開,使用下面的設置就不會出現這個問題了,直接打開。

AddType application/octet-stream .pdf

AddType application/octet-stream .zip

AddType application/octet-stream .mov

 

口令認證– 你可以創建一個文件用於認證。下面是一個示例:

# to protect a file

AuthType Basic

AuthName “Prompt”

AuthUserFile /home/path/.htpasswd

Require valid-user

 

 

修改文件類型– 下面的示例可以讓任何的文件都成爲PHP那麼被服務器解釋。比如:myphp, cgi,phtml等。

ForceType application/x-httpd-php

SetHandler application/x-httpd-php

 

阻止存取.htaccess 文件

# secure htaccess file

order allow,deny

deny from all

 

保護服務器上的文件被存取

# prevent access of a certain file orderallow,deny

deny from all

 

阻止目錄瀏覽

# disable directory browsing

Options All -Indexes

 

設置默認主頁

# serve alternate default index page

DirectoryIndex about.html

 

爲服務器管理員設置電子郵件。

ServerSignature EMail

SetEnv SERVER_ADMIN [email protected]

 

使用.htaccess 訪止盜鏈。如果你網站上的一個圖片被別的N多的網站引用了,那麼,這很有可能會導致你服務器的性能下降,使用下面的代碼可以保護某些熱門的鏈接不被過多的引用。

Options +FollowSymlinks

# Protect Hotlinking

RewriteEngine On

RewriteCond %{HTTP_REFERER} !^$

RewriteCond %{HTTP_REFERER} !^http://(www.)?domainname.com/[nc]

RewriteRule .*.(gif|jpg|png)$http://domainname.com/img/hotlink_f_o.png [nc]

 

# password-protect a directory

resides

AuthType basic

AuthName “This directory is protected”

AuthUserFile /home/path/.htpasswd

AuthGroupFile /dev/null

Require valid-user

 

把老的域名轉像新的域名

# redirect from old domain to new domain

RewriteEngine On

RewriteRule ^(.*)$http://www.yourdomain.com/$1 [R=301,L]

 

本文爲Anyforweb技術分享博客,需要了解網站建設及更多web應用相關信息,請訪問anyforweb.com。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章