Heim > php教程 > php手册 > Hauptteil

htaccess 防止盗链,防止目录浏览等10大技巧

WBOY
Freigeben: 2016-06-13 10:48:35
Original
849 Leute haben es durchsucht

1. 反盗链
那些盗用了你的内容,还不愿意自己存储图片的网站是无耻的。你可以通过以下配置来放置别人盗用你的图片:
 
1 RewriteBase /
2 RewriteCond %{HTTP_REFERER} !^$
3 RewriteCond %{HTTP_REFERER} !^http://(www.)?yoursite.com/.*$ [NC]
4 RewriteRule .(gif|jpg|swf|flv|png)$ /feed/ [R=302,L]
2. 防止目录浏览
有时候目录浏览是有用的,但大部分情况会有安全问题。为了让你的网站更安全,你可以通过htaccess文件来禁用这个功能:
 
1 Options All -Indexes


3. SEO友好的301永久重定向
这一招是我常用的。每次我更改网站URL结构的时候,我都会做301重定向:
 
1 Redirect 301 http://www.yoursite.com/article.html http://www.yoursite.com/archives/article
4. 显示个性化的 404 错误页面
当用户访问了一个不存在的页面的时候,网页服务器会显示“404 file not found”错误。有很多CMS可以让你设置自定义的错误页面,但最简单的方法是更改htaccess:
 
1 ErrorDocument 404 /404.html


5. 设置目录的默认页面
假如你需要为不同的目录设置不同的默认页面,你可以很容易的通过 .htaccess 实现:
 
1 DirectoryIndex about.html
6. 基于referer来限制网站访问
站长通常不会限制网站访问,但是当你发现有一些网站尽给你带来垃圾流量的话,你就应该屏蔽他们:
 

2 RewriteEngine on  RewriteCond %{HTTP_REFERER} spamteam.com [NC,OR]
3 RewriteCond %{HTTP_REFERER} trollteam.com [NC,OR]
4 RewriteRule .* – [F]

7. 限制PHP上传文件大小
这招在共享空间的服务器上很有用,可以让我的用户上传更大的文件。第一个是设置最大的上传文件大小,第二个是设置最大的POST请求大小,第三个PHP脚本最长的执行时间,最后一个是脚本解析上传文件的最长时间:
 
1 php_value upload_max_filesize 20M
2 php_value post_max_size 20M
3 php_value max_execution_time 200
4 php_value max_input_time 200


8. 压缩文件
你可以通过压缩文件来减少网络流量,也页面装载时间:
 
1 AddOutputFilterByType DEFLATE text/plain
2 AddOutputFilterByType DEFLATE text/html
3 AddOutputFilterByType DEFLATE text/xml
4 AddOutputFilterByType DEFLATE text/css
5 AddOutputFilterByType DEFLATE application/xml
6 AddOutputFilterByType DEFLATE application/xhtml+xml
7 AddOutputFilterByType DEFLATE application/rss+xml
8 AddOutputFilterByType DEFLATE application/javascript
9 AddOutputFilterByType DEFLATE application/x-javascript
9. 缓存文件
这一点还需要解释吗?
 

2 Header set Cache-Control “max-age=2592000″

10. 添加尾部的反斜杠
我并不确定,但是很多文章,很多人都说添加尾部反斜杠有益于SEO:
 

2 RewriteCond %{REQUEST_URI} /+[^\.]+$
3 RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

 

摘自  PainsOnline的专栏 
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!