10个实用的.htaccess代码片段分别是什么

这篇文章将为大家详细讲解有关10个实用的.htaccess代码片段分别是什么,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

下面介绍了10个实用的.htaccess代码片段。

1、除 URL 中的 www

出于 SEO 考虑,你可能期望移除 URL 中的 www 前缀。以下代码实现了这个功能,并将所有带 www 的地址重定向到无 www 一级域名。

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

来源:http://css-tricks.com/snippets/htaccess/www-no-www/

2、防止盗链

盗链通常被认为是可耻行为。当你被别人盗链,别人将免费使用你那昂贵的带宽,不是小气,是带宽费用伤不起啊伤不起。要防止盗链仅需添加使用以下代码:

RewriteEngine On  #将 ?mangguo\.org/ 替换成你的博客地址  RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mangguo\.org/ [NC]  RewriteCond %{HTTP_REFERER} !^$  #将 /images/nohotlink.jpg 替换成“请勿盗链”图片地址  RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]

3、将 WordPress RSS 源重定向到 Feedburner

大多数博客作者使用 Feedburner 托管 RSS 种子,以便对博客阅读进行统计分析。如果你使用 WordPress,你应当会将所有 RSS 订阅源重定向到 Feedburner 源。修改第二行和第三行代码,并将代码拷贝到 .htaccess 中。

<IfModule mod_alias.c>  RedirectMatch 301 /feed/(atom|rdf|rss|rss2)/?$ http://feeds.feedburner.com/mangguo/  RedirectMatch 301 /comments/feed/(atom|rdf|rss|rss2)/?$ http://feeds.feedburner.com/mangguo/  </IfModule>

来源:http://www.wprecipes.com/how-to-redirect-wordpress-rss-feeds-to-feedburner-with-htaccess

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

来源:http://css-tricks.com/snippets/htaccess/custom-error-pages/

5、强制下载指定文件

当提供一些类似 MP3、eps 或 xls 文件下载时,你可能需要强制让客户端下载而不是让浏览器决定是不是要下载。

<Files *.xls> ForceType application/octet-stream  Header set Content-Disposition attachment  </Files> <Files *.eps> ForceType application/octet-stream  Header set Content-Disposition attachment  </Files>

来源:http://www.givegoodweb.com/post/30/forcing-a-download-with-apache-and-htaccess

6、记录 PHP 错误

这段代码将在服务器上创建一个 php_error.log 文件,并将 PHP 文件的错误记录写入该日志文件。

# 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

来源:http://css-tricks.com/snippets/htaccess/php-error-logging/

7、移除 URL 中的文件扩展名

文件扩展名对开发者可能有用,但对于访客而言,根本毛都没用。这段代码将移除 html 文件那一坨一坨的 .html 后缀。当然你也可以用于移除其他类型的文件,比如 php 等。

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

来源:http://eisabainyo.net/weblog/2007/08/19/removing-file-extension-via-htaccess

8、防止目录列表

在你的 web 服务器上,当一个目录没有索引文件,apache 自动会为当前目录中所有文件创建索引列表。如果你不希望别人看到这些文件,可以添加以下代码来阻止自动目录列表。

9、Options -Indexes通过压缩静态资源减少页面大小

浏览器中的数据传输是可以被压缩的,客户端能够解压服务端发送的压缩数据。这段代码将友好地减少你的页面大小,并节约带宽开支。

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 编码

为了避免编码问题,你可以通过 .htaccess 文件强制指定编码。这样一来,就可以确保 HTML 文档总能被正确渲染,即便你忘了添加 <meta http-equiv="Content-Type"> 语句。

<FilesMatch "\.(htm|html|css|js)$">  AddDefaultCharset UTF-8  </FilesMatch>

关于10个实用的.htaccess代码片段分别是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。