Htaccess 设置小技巧

前面写了一篇用htaccess防止图片盗链的文章, 这次再总结一些htaccess文件的其他用处:

  • 设置时区

SetEnv TZ America/Chicago

时区的列表可以在这里找到: http://us2.php.net/manual/en/timezones.php

  • 链接扩展名 - Different File Extension

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*).zig$ $1.php [nc]

这样, 访问example.zig就相当于example.php

  • 取消扩展名

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]

这样做以后, 网址URL中不会显示.php这个后缀

  • 拒绝访问包含文件 - Deny Access to Include Files

<FILES ~ “.inc$”>
Order allow,deny
Deny from all
</FILES>

阻止其他人浏览inc文件, 有点疑惑, 貌似是禁止访问inc文件夹中的所有文件.

  • 拒绝文件夹列表

Options -Indexes

如果文件夹中没有index文件的话, 这个设置会拒绝其他人浏览文件夹的内容.

  • 404转向, 重定向

ErrorDocument 404 http://exmple.com/

很明显, 将404错误页面设置(转到)为http://exmple.com/ (原文如此, 明显是把example错误拼写为exmple了)

  • 加快网页加载时间

<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl|jpg|png|gif)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>

需要有mod_czip.c模块的支持, 可以将传输的内容压缩55-65%, 从而提高35-40%的速度(当然都是理想状态)

  • 显示站点维护状态页面

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteRule $ /maintenance.html [R=302,L]

用上述命令, 网站浏览者将会被带到maintenance.html这个页面, 在维护网站的时候很有用.

翻译的并不是很准确, 所以附上了部分英文, 这些小技巧来全部翻译自dreamhost的wiki. 互联网上还有其他的资源可以参考, 推荐以下两篇:

中文: http://hellobmw.com/archives/using-htaccess.html
英文: http://www.freewebmasterhelp.com/tutorials/htaccess/1

关于本文:

您看到的这篇文章由i.robot发表于2008年3月17日.

归类于: Blogging, Linux

留言时请填写名字, email您想说的话, 然后点击黄色的提交按钮.

您的邮件地址对外保密, 请放心.

读者留言

  1. 环保袋, 10:37 am - 19 Apr, 2008 [回复]

    good

Leave a Reply

Note: Commenter is allowed to use '@User+blank' to automatically notify your reply to other commenter. e.g, if ABC is one of commenter of this post, then write '@ABC '(exclude ') will automatically send your comment to ABC. Using '@all ' to notify all previous commenters. Be sure that the value of User should exactly match with commenter's name (case sensitive).