简单实用的.htaccess技巧

1. 允许目录浏览

# Enable Directory Browsing
Options +Indexes

2. 阻止目录浏览

# Disable Directory Browsing
Options All -Indexes

3. 创建自定义的出错页面

ErrorDocument 403 /forbidden.html
ErrorDocument 404 /notfound.html
ErrorDocument 500 /servererror.html

4. 添加对HTML/SHTML的支持

AddType text/html .html
AddType text/html .shtml
AddHandler server-parsed .html
AddHandler server-parsed .shtml

5. 设置默认主页(按顺序)

DirectoryIndex myhome.htm index.htm index.php

6. 阻止某些用户

<limit GET POST PUT>
order deny,allow
deny from 202.54.122.33
deny from 8.70.44.53
deny from .spammers.com
allow from all
</limit>

7. 只允许局域网用户

order deny,allow
deny from all
allow from 192.168.0.0/24

8. 重定向文件或者目录

Redirect oldpage.html http://www.domainname.com/newpage.html
Redirect /olddir http://www.domainname.com/newdir/

9. 阻止某些网站

RewriteEngine on
RewriteCond %{HTTP_REFERER} site-to-block\.com [NC]
RewriteCond %{HTTP_REFERER} site-to-block-2\.com [NC]
RewriteRule .* – [F]

10. 防止盗链

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC]
RewriteRule \.(gif|jpg)$ – [F]

11. 阻止存取文件

<files file-name>
order allow,deny
deny from all
</files>

12. 允许运行CGI脚本

Options +ExecCGI
AddHandler cgi-script cgi pl

13. 密码保护

# 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

14. 开启 GZIP

# BEGIN GZIP
<ifmodule mod_deflate.c>
# Combine the below two lines – I’ve split it up for presentation
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css
application/x-javascript application/javascript
</ifmodule>
# END GZIP

15. 关闭 magic_quotes_gpc

# Only if you use PHP
<ifmodule mod_php4.c>
php_flag magic_quotes_gpc off
</ifmodule>

16. 设置网站的时区

SetEnv TZ America/Houston

17. 为服务器管理员设置电子邮件

ServerSignature EMail
SetEnv SERVER_ADMIN default@domain.com

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

19. 直接打开文件而不是下载

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

20. 把老的域名转向新的域名

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

  1. 支持,支持! 呵呵!

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>