代码改变世界

10个常用的.htaccess配置技巧

2012-02-24 00:26  林爵  阅读(273)  评论(0)    收藏  举报

1、反盗链

那些盗用了你的内容,还不愿意自己存储内容的网站是可耻的。你可以通过以下配置来防止别人盗用你的内容。

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

 

2、防止目录浏览

有时候目录浏览是有用的,但大部分情况会有安全问题。为了让你的网站更安全,你可以通过.htaccess文件禁用此功能。

1 Options All -Indexes

 

3、SEO友好的301永久重定向

1 Redirect 302 http://www.wilead.com/301.html  http://www.wilead.com/article

 

4、显示个性化的404错误页面

当用户访问了一个不存在的页面时,网页服务器会显示“404 File not found”错误。可以通过修改.htaccess文件来自定义错误页面。

1 ErrorDocument 404 /404.html

 

5、设置目录的默认页面

1 DirectoryIndex index.html

 

6、基于referer来限制网站访问

如果发现有来自与其它站点(通常是抓取行为)的垃圾流量,则应该果断屏蔽

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_REFERER} spamteam.com [NC,OR]
RewriteCond %{HTTP_REFERER} trollteam.com [NC,OR]
RewriteRule .* - [F]
</ifModule>

 

7、限制PHP上传文件大小

# 最大上传文件大小
php_value upload_max_filesize 20M
# 最大的POST请求大小
php_value post_max_size 20M
# PHP脚本最长的执行时间
php_value max_execution_time 200
# 脚本解析上传文件的最长时间
php_value max_input_time 200

 

8、压缩文件

AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

 

9、缓存文件

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

 

10、添加尾部的反斜杠

听说这样做有益于SEO

1 <IfModule mod_rewrite.c>
2 RewriteCond %{REQUEST_URI} /+[^\.]+$
3 RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
4 </IfModule>