10个经典.htaccess应用

10大经典htaccess应用

1、去掉网站链接(url)中的www

代码:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^your-site.com$ [NC]
RewriteRule ^(.*)$ http://your-site.com/$1 [L,R=301]



2、防盗链设置

代码:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^your-site.com$ [NC]
RewriteRule ^(.*)$ http://your-site.com/$1 [L,R=301]

更详尽的防盗链方法请点击“利用.htaccess防盗链设置

3、重定向RSS/Feed 到Feedburner

代码:


RedirectMatch 301 /feed/(atom|rdf|rss|rss2)/?$ http://feedburner.com/yourfeed/
RedirectMatch 301 /comments/feed/(atom|rdf|rss|rss2)/?$ http://feedburner.com/yourfeed/



4、自定义错误页面

代码:

ErrorDocument 400 /errors/badrequest.html
ErrorDocument 401 /errors/authreqd.html
ErrorDocument 403 /errors/forbid.html
ErrorDocument 404 /errors/notfound.html
ErrorDocument 500 /errors/serverr.html

详情请看“漂亮的404页面


5、强制现在特定的文件
有些浏览器对xls、eps、html、txt、xml等类型的文件默认是打开,而我们又想让用户下载,那么此时就需要进行强制下载的设置

代码:


ForceType application/octet-stream
Header set Content-Disposition attachment


ForceType application/octet-stream
Header set Content-Disposition attachment



6、php错误日志
这是一个有趣的添加php错误日志的方式,你只要在服务器的某个位置创建php_error.log文件,并且将此文件的路径添加到htaccess文件就OK了

代码:

# display no errs to user
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
# log to file
php_flag log_errors on
php_value error_log /location/to/php_error.log



7、隐藏url中的文件扩展名

代码:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
# Replace html with your file extension, eg: php, htm, asp



8、隐藏服务器中文件列表
如果你不想让别人看到你的目录文件,不想让人知道你的文件列表,那么就可以用下面一句代码实现

代码:

Options -Indexes



9、对静态数据进行压缩,减少网页的重量,加快访问速度

代码:

AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html



10、自动添加utf-8编码集到文件
自动添加utf-8编码到文件,防止因编码问题出现乱码现象

代码:


AddDefaultCharset UTF-8

原文来源:http://www.catswhocode.com/blog/10-useful-htaccess-snippets-to-have-in-your-toolbox

未经允许不得转载:WEIXING.ME » 10个经典.htaccess应用

相关文章

评论 (0)

  1. avatar

    不错,很全,我只了解过301跳转之类的应用

    • avatar
      WEIXING06-11 17:38回复

      其实我用过的也不多,写出来让需要使用的人能找到。

  2. avatar
    jfbcb06-11 14:47回复

    大哥,,,写得很好,小弟,表膜拜,,,,弱弱地问下,可以交换友情 么,FLY's SKY ,jfbcb.com

    • avatar
      WEIXING06-11 16:46回复

      呵呵,过奖了,一起学习。链接加好了!

  3. avatar

    每天坚持分享一篇这样的文章,博客就有看的啦

  4. avatar
    BOSS06-13 14:23回复

    这个wordpress主题哪有,我也想要!

  5. avatar
    美家具06-14 10:35回复

    了解下技术上的东西!

  6. avatar

    分析的很好哦,博主真good
    以后没事经常来逛逛。。。 嘿嘿

  7. avatar

    我也曾很努力地寻找过关于这个文件的应用方法!