日期:2014-05-16  浏览次数:20888 次

12条htaccess基础技巧

1.创建自定义错误页(Create a custom error page)

Linux?Apache服务器的.htaccess文件让你很容易创建自定义错误页。现在创建你的自定义错误页并将下面代码加入到.htaccess文件中:

ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php

你可以替换代码中/500.php文件的名字及路径。

2.禁止目录浏览(Prevent directory browsing)

如果你的网站目录没有包含一个启动文档(如:index.html,index.htm,index.asp,index.php等等),那么网站就会自动启动浏览目录的功能,这很危险,所以需要禁止,禁止方法也很简单,只需要在.htaccess中添加如下代码:

Options All -Indexes

3.设置每个目录的启动文档(Set the default page of each directory)

如果你不想使用默认的启动文档(如:index.html,index.htm,index.asp,index.php等等),那么你可以指定你希望启动的任何文档,如一个关于文档,在.htaccess中添加如下代码:

DirectoryIndex news.html

当然,你可以将news.html文件改为任何你想要打开的文件。

4.设置301重定向(Set up a 301 redirect)

假如你修改了网站的结构想将旧的网址重定向到新的位置,下面的代码回帮到你:

Redirect 301 /original/filename.html http://domain.com/updated/filename.html

5.使用GZIP压缩输出文件(Compress file output with GZIP)