分享10个实用的.htaccess代码片段

开发 前端
本文介绍的是.htaccess的代码片段,可是很实用的呢。希望对你有帮助,一起来看。

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

1、除 URL 中的 www

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

 

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

 

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

2、防止盗链

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

 

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

 

3、将 WordPress RSS 源重定向到 Feedburner

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

 

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

 

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

4、创建自定义错误页

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

 

  1. ErrorDocument 400 /errors/badrequest.html  
  2. ErrorDocument 401 /errors/authreqd.html  
  3. ErrorDocument 403 /errors/forbid.html  
  4. ErrorDocument 404 /errors/notfound.html  
  5. ErrorDocument 500 /errors/serverr.html 

 

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

5、强制下载指定文件

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

 

  1. <Files *.xls> 
  2. ForceType application/octet-stream  
  3. Header set Content-Disposition attachment  
  4. </Files> 
  5. <Files *.eps> 
  6. ForceType application/octet-stream  
  7. Header set Content-Disposition attachment  
  8. </Files> 

 

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

6、记录 PHP 错误

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

 

  1. # display no errs to user  
  2. php_flag display_startup_errors off  
  3. php_flag display_errors off  
  4. php_flag html_errors off  
  5. # log to file  
  6. php_flag log_errors on  
  7. php_value error_log /location/to/php_error.log 

 

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

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

 

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

 

  1. RewriteEngine on  
  2. RewriteCond %{REQUEST_FILENAME} !-d  
  3. RewriteCond %{REQUEST_FILENAME}\.html -f  
  4. RewriteRule ^(.*)$ $1.html  
  5. # 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通过压缩静态资源减少页面大小

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

 

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

 

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

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

 

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

 

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

希望本文的介绍,能够给你带来帮助。

【编辑推荐】

  1. 浅谈XML在Web中的应用
  2. Web 2.0 用户界面技术
  3. 2011年Web开发者必须掌握的10大技能
  4. ASP.NET配置文件Web.config详细解释
  5. web开发:如何使你的作品展示优于竞争对手
责任编辑:于铁 来源: 芒果
相关推荐

2020-11-16 16:04:42

CSS设计代码

2015-10-08 08:53:46

PHP代码片段

2011-07-14 10:07:19

PHP

2021-09-17 15:31:47

代码JavaScript数组

2021-10-31 07:36:17

前端JavaScript编程

2015-11-02 09:25:07

jQuery代码片段

2011-11-23 09:21:43

jQuery

2011-07-11 10:16:07

JavaScript

2015-08-19 09:15:11

C#程序员实用代码

2023-06-16 16:34:25

JavaScripWeb 开发

2024-01-04 16:46:58

JavaScript开发

2023-11-03 16:02:00

JavaScript开发

2024-04-09 00:00:00

Java代码片段

2015-10-29 10:30:41

C#程序员实用代码

2018-05-10 15:35:03

前端代码图像

2012-11-27 10:23:18

CSSWeb开发

2019-10-10 16:49:18

Python镜音双子脚本语言

2022-06-08 08:55:15

JavaScript代码前端

2011-09-06 15:16:42

PHP

2023-10-12 15:02:21

PythonPandas数据分析
点赞
收藏

51CTO技术栈公众号