16個簡單實用的.htaccess技巧

.htaccess 文件 (Hypertext Access file) 是Apache Web服務器的一個很是強大的配置文件,對於這個文件,Apache有一堆參數可讓你配置出幾乎爲所欲爲的功能。.htaccess 配置文件堅持了Unix的一個文化——使用一個ASCII 的純文本文件來配置你的網站的訪問策略。php

這篇文章包括了16個很是有用的小技巧。另外,由於.htaccess 是一個至關強大的配置文件,因此,一個輕微的語法錯誤會形成你整個網站的故障,因此,在你修改或是替換原有的文件時,必定要備份舊的文件,以便出現問題的時候能夠方便的恢復。html

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

ErrorDocument 401 /error/401.php
ErrorDocument 403 /error/403.php
ErrorDocument 404 /error/404.php
ErrorDocument 500 /error/500.phplinux

2. 設置網站的時區web

SetEnv TZ America/Houstonapache

 

3. 阻止IP列表
有些時候,你須要以IP地址的方式阻止一些訪問。不管是對於一個IP地址仍是一個網段,這都是一件很是簡單的事情,以下所示:服務器

allow from all
deny from 145.186.14.122
deny from 124.15app

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

4. 把一些老的連接轉到新的連接上——搜索引擎優化SEOide

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

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

ServerSignature EMail
SetEnv SERVER_ADMIN default@domain.com

6. 使用.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]

7. 阻止 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

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

ErrorDocument 403 http://www.youdomain.com
Order deny,allow
Deny from all
Allow from ip
Allow from ip

9. 直接找開文件而不是下載 – 一般,咱們打開網上文件的時候老是會出現一個對話框問咱們是下載仍是直接打開,使用下面的設置就不會出現這個問題了,直接打開

AddType application/octet-stream .pdf
AddType application/octet-stream .zip
AddType application/octet-stream .mov

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

ForceType application/x-httpd-php
SetHandler application/x-httpd-php

11. 阻止存取.htaccess 文件

# secure htaccess file
order allow,deny
deny from all

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

# prevent access of a certain file order allow,deny
deny from all

13. 阻止目錄瀏覽

# disable directory browsing
Options All -Indexes

14. 設置默認主頁

# serve alternate default index page
DirectoryIndex about.html

15. 口令認證 – 你能夠建立一個文件用於認證。下面是一個示例:

# to protect a file

AuthType Basic
AuthName 「Prompt」
AuthUserFile /home/path/.htpasswd
Require valid-user

# password-protect a directory
resides
AuthType basic
AuthName 「This directory is protected」
AuthUserFile /home/path/.htpasswd
AuthGroupFile /dev/null
Require valid-user

16. 把老的域名轉像新的域名

# redirect from old domain to new domain
RewriteEngine On
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]

來源:http://rafeekphp.wordpress.com/2009/06/06/16-great-htaccess-tricks-and-hacks/

相關文章
相關標籤/搜索