17個htaccess設定

htaccess

htaccess 檔是apache http 伺服器上的一個設定檔,您可以藉由htaccess來調整或加強網站的相關設定,下面就列出一些好用的htaccess設定語法。
1.讓IE使用最新的模式瀏覽

[code]
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
BrowserMatch MSIE ie
Header set X-UA-Compatible "IE=Edge,chrome=1" env=ie
</IfModule>
</IfModule>
[/code]

2.301永久轉址

[code]
Redirect 301 http://www.wespai.com/home http://www.wespai.com/
[/code]

3.略過下載提示直截下載

[code]
AddType application/octet-stream .pdf
AddType application/octet-stream .zip
AddType application/octet-stream .mov
[/code]

4.開啟Gzip

[code]
<IfModule mod_deflate.c>

# html, xml, css, and js:
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/x-javascript text/javascript application/javascript application/json

# webfonts and svg:
<FilesMatch "\.(ttf|otf|eot|svg)$" >
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
[/code]

5.自訂錯誤網頁

[code]
ErrorDocument 401 /error/401.php
ErrorDocument 403 /error/403.php
ErrorDocument 404 /error/404.php
ErrorDocument 500 /error/500.php
[/code]

6.將www.xxx.xxx轉向xxx.xxx,去掉不必要的www網址

[code]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
[/code]

7.開啟cache相關檔案,加快開啟速度

[code]
<FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>
[/code]

8.對於常變動的檔案不開啟cache

[code]
<FilesMatch ".(pl|php|cgi|spl|scgi|fcgi)$">
Header unset Cache-Control
</FilesMatch>
[/code]

9.避免在別的網域使用網站資源,例如:讓圖片無法在別的網站顯示

[code]
RewriteBase /
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?wespai.com/.*$ [NC]
RewriteRule .(gif|jpg|swf|flv|png)$ /feed/ [R=302,L]
[/code]

10.避免常見的駭客攻擊

[code]
RewriteEngine On

# proc/self/environ? no way!
RewriteCond %{QUERY_STRING} proc/self/environ [OR]

# Block out any script trying to set a mosConfig value through the URL
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]

# Block out any script trying to base64_encode crap to send via URL
RewriteCond %{QUERY_STRING} base64_encode.*(.*) [OR]

# Block out any script that includes a <script> tag in URL
RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]

# Block out any script trying to set a PHP GLOBALS variable via URL
RewriteCond %{QUERY_STRING} GLOBALS(=|[|\%[0-9A-Z]{0,2}) [OR]

# Block out any script trying to modify a _REQUEST variable via URL
RewriteCond %{QUERY_STRING} _REQUEST(=|[|\%[0-9A-Z]{0,2})

# Send all blocked request to homepage with 403 Forbidden error!
RewriteRule ^(.*)$ index.php [F,L]
[/code]

11.禁止讀取設定的檔案

[code]
# secure htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>

# prevent viewing of a specific file
<Files secretfile.jpg>
order allow,deny
deny from all
</Files>

# multiple file types
<FilesMatch ".(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$">
Order Allow,Deny
Deny from all
</FilesMatch>
[/code]

12.重新命名htaccess

[code]
AccessFileName htacc.ess
[/code]

13.關閉或開啟瀏覽目錄

[code]
# disable directory browsing
Options All -Indexes

# enable directory browsing
Options All +Indexes
[/code]

14.改變域設讀取網頁

[code]
DirectoryIndex business.html
[/code]

15.禁止從某網域過來瀏覽

[code]
# block visitors referred from indicated domains
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_REFERER} scumbag.com [NC,OR]
RewriteCond %{HTTP_REFERER} wormhole.com [NC,OR]
RewriteRule .* – [F]

</ifModule>
[/code]

16.從User-Agent Header類型阻擋瀏覽

[code]
# block visitors referred from indicated domains
<IfModule mod_rewrite.c>
SetEnvIfNoCase ^User-Agent$ .*(craftbot|download|extract|stripper|sucker|ninja|clshttp|webspider|leacher|collector|grabber|webpictures) HTTP_SAFE_BADBOT
SetEnvIfNoCase ^User-Agent$ .*(libwww-perl|aesop_com_spiderman) HTTP_SAFE_BADBOT
Deny from env=HTTP_SAFE_BADBOT
</ifModule>
[/code]

17.禁止此目錄下執行script,通常用於機密的目錄

[code]
# secure directory by disabling script execution
AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi
Options -ExecCGI
[/code]

最新留言

  1. 百里幻雲 2010-12-09

留個言吧

防止機器人留言: