善用.htaccess文件成为网站利器
既然是技术人员,那么实际的代码是必不可少的。鉴于目前很多的站点大多数使用的都是以PHP+MYSQL为主流的网站程序,那么网站的配置文件是必须受到我们格外重视的。本文为笔者收集和整理了几乎所有.htaccess文件的配置使用方法及其作用,收集来源于互联网。
【301永久重定向】
使用301永久重定向避免www与根域名的权重是非常好的习惯和方法,但是从http头来进行设置有亮点不好,一是增加网页的http响应次数,延长 页面的响应时间,二是比较容易设置成302临时重定向。所以笔者建议使用.htaccess文件来进行网站的301永久重定向。
为什么这是搜索引擎友好的呢?因为现在很多现代的搜索引擎都有能根据检查301永久转向来更新它现有的记录的功能。
代码如下(以笔者的博客www.AAA.com为例):
RewriteEngine onRewriteCond %{http_host} ^AAA.com [NC]RewriteRule ^(.*)$ http://www.AAA.com/$1 [L,R=301]
RewriteEngine onRewriteCond %{http_host} ^ www.AAA.com [NC]RewriteRule ^(.*)$ http://AAA.com/$1 [L,R=301]
ErrorDocument 401 /error/401.phpErrorDocument 403 /error/403.phpErrorDocument 404 /error/404.phpErrorDocument 500 /error/500.php
AddOutputFilterByType DEFLATE text/plainAddOutputFilterByType DEFLATE text/htmlAddOutputFilterByType DEFLATE text/xmlAddOutputFilterByType DEFLATE text/cssAddOutputFilterByType DEFLATE application/xmlAddOutputFilterByType DEFLATE application/xhtml+xmlAddOutputFilterByType DEFLATE application/rss+xmlAddOutputFilterByType DEFLATE application/javacriptAddOutputFilterByType DEFLATE application/x-javascript
ExpiresActive on //开启缓存ExpiresByType text/css "access 1 month"//css文件缓存1个月ExpiresByType text/plain "access 2 days"//纯文本内容缓存2天ExpiresByType text/html "access 2 days"//html文件缓存2天ExpiresByType application/javascript "access 1 month"//JS文件缓存1个月ExpiresByType image/jpeg "access 1 month"//jpeg图片缓存1个月ExpiresByType image/x-icon "access 1 month"//icon缓存1个月ExpiresByType image/gif "access 1 month"//gif图片缓存一个月ExpiresByType image/png "access 1 month"//png图片缓存一个月ExpiresByType image/ico "access 1 month"//ico缓存1个月ExpiresByType application/pdf "access 1 month"//pdf文件缓存一个月ExpiresByType application/x-shockwave-flash "access 1 month"//flash缓存一个月ExpiresDefault "access 1 month"// 默认(未提及的可缓存文件)缓存1个月
Header unset Cache-Control
deny from 000.000.000.000
allow from 000.000.000.000
deny from all
RewriteBase /RewriteCond %{HTTP_REFERER} !^$RewriteCond %{HTTP_REFERER} !^http://(www.)?aqee.net/.*$ [NC]RewriteRule .(gif|jpg|swf|flv|png)$ /feed/ [R=302,L]