10 个实用的 .htaccess 代码片段

作者: aries 分类: PHP 发布时间: 2011-06-13 08:00 ė 1047次浏览 6 0评论

谨记:在编辑 .htaccess 之前记得备份哦!

移除 URL 中的 www

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

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

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

防止盗链

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

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

将 WordPress RSS 源重定向到 Feedburner

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

RedirectMatch 301 /feed/(atom¦rdf¦rss¦rss2)/?$ http://feeds.feedburner.com/iw3c/
RedirectMatch 301 /comments/feed/(atom¦rdf¦rss¦rss2)/?$ http://feeds.feedburner.com/iw3c/

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

创建自定义错误页

看烦了老旧的错误页面?那就亲手实践下制作自定义错误页吧。将这些个性错误页上传到主机,然后添加以下代码:

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/


强制下载指定文件

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

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

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


记录 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/


移除 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

防止目录列表

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

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

自动为文件添加 utf-8 编码

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

AddDefaultCharset UTF-8

来源:http://www.askapache.com/htaccess/setting-charset-in-htaccess.html

英文原稿:10 useful .htaccess snippets to have in your toolbox | CatsWhoCode
翻译整理:10 个实用的 .htaccess 代码片段 | 芒果小站
换一个
暂无评论
Ɣ回顶部