.htaccess文件是Apache服务器中非常重要的一个配置文件,一般情况下存在于网站的根目录,传统静态网站需要手工创建.htaccess文件并进行规则书写; 现在较为先进的建站程序如 WordPress、Joomla!都可以自动生成.htacces文件并进行相关规则的自动书写。htaccess文件可以帮我们实现很多功能,极大的扩展虚拟主机的功能:
- 伪静态
- 网页301重定向
- 自定义404错误页面
- 改变文件扩展名
- 允许/阻止特定的用户或者目录的访问
- ......
下面总结一下笔者常用的针对WordPress的 .htaccess文件优化技巧, 部分技巧具有通用性 :
1. 重定向index.php到主域名(WordPress一般会自动书写)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
2. 保护 .htaccess 自身的安全性,阻止用户通过读取和写入 .htaceess.
<files .htaccess> order allow,deny deny from all </files>
3. 禁止浏览目录这将阻止服务器在没有 index 文件(如 index.html,index.php 等等)的情况下显示文件夹目录内容,阻止用户看到文件夹的内容使得更难对网站发动攻击
# disable directory browsing Options All -Indexes
4. 保护 wp-config.php 文件,阻止用户读取和写入 WordPress 的配置文件。这里假设WordPress 是和.htaccess文件在同一个目录(根目录)的。
<files wp-config.php> Order deny,allow deny from all </files>
5. 给特定类型文件指定 Vary: Accept-Encoding header,参阅【WordPress优化之Specify Vary: Accept-Encoding header】
<IfModule mod_headers.c> <FilesMatch ".(js|css|xml|gz)$"> Header append Vary Accept-Encoding </FilesMatch> </IfModule>
6.给php文件指明Last-Modified header,参阅【WordPress优化之Specify a cache validator】
<IfModule mod_headers.c> <FilesMatch "\.(php)$"> Header set Last-Modified "Tue, 31 Aug 2010 00:00:00 GMT" </FilesMatch> </IfModule>
7.去除Etag.
FileETag none
8.设置浏览器缓存,参阅【WordPress优化之设置浏览器端缓存:Leverage browser caching】
#Expire Header <IfModule mod_expires.c> ExpiresActive on ExpiresByType text/css "access 1 month" ExpiresByType text/plain "access 2 days" ExpiresByType text/html "access 2 days" ExpiresByType application/javascript "access 1 month" ExpiresByType image/jpeg "access 1 month" ExpiresByType image/x-icon "access 1 month" ExpiresByType image/gif "access 1 month" ExpiresByType image/png "access 1 month" ExpiresByType image/ico "access 1 month" ExpiresByType application/pdf "access 1 month" ExpiresByType application/x-shockwave-flash "access 1 month" ExpiresDefault "access 1 month" </IfModule>
注:
.htaccess文件中的配置指令作用于.htaccess文件所在的目录及其所有子目录
子目录中的.htaccess指令会覆盖父目录中的.htaccess指令
您可能也喜欢这些文章:
- 2012/03/29 — 纯代码替换All in One SEO Pack 插件,实现SEO优化 (7)
- 2012/03/16 — WordPress配置文件wp-config.php实用技巧 (3)
- 2011/06/02 — 浅谈WordPress的Pingback和TrackBack机制及其优化 (4)
- 2011/04/19 — WordPress优化之Specify a cache validator (4)
- 2011/04/19 — WordPress优化之设置浏览器端缓存:Leverage browser caching (3)
- 2011/02/23 — WordPress优化之Specify Vary: Accept-Encoding header (2)
- 2011/07/02 — 纯代码实现WordPress热门文章功能 (6)
- 2011/06/22 — 纯代码实现WordPress相关文章功能 (13)

{ 4 Comments }
I loved as much as you will receive carried out right here. The sketch is tasteful, your authored material stylish. nonetheless, you command get got an impatience over that you wish be delivering the following. unwell unquestionably come further formerly again as exactly the same nearly very often inside case you shield this increase.
收藏了,这种文章不是看的,是得收藏起来,以后慢慢看的。
我觉得这个文件就有俩比较有用的功能,301转向和链接重写。
NGINX表示 很不淡定
nginx不太清楚啊,现在的虚拟主机都是Apache的,nginx只能在VPS上面折腾啦。