ErrorDocument 404 /404.html
保存成.htaccess文件上传到网站的根目录。
/404.html是目录名和文件名,能够改为本身的名字。
QUOTE:
.htaccess文件是很是有用的
• Part 1 – Introduction介绍
• Part 2 - .htaccess Commande命令
• Part 3 - Password protection密码保护
Part 1 – Introduction介绍
Introduction 介绍
In this tutorial you will find out about the .htaccess file and thepower it has to improve your website. Although .htaccess is only afile, it can change settings on the servers and allow you to domany different things, the most popular being able to have your owncustom 404 error pages. .htaccess isn't difficult to use and isreally just made up of a few simple instructions in a textfile.
从本指南中,您能够学习到有关.htaccess文档及其功能,用以优化您的网站。尽管.htaccess只是一个文档,但它能够更改服务器的设置,容许您作许多不一样的事情,最流行的功能是您能够建立自定义的“404error”页面。.htaccess 并不难于应用,只是在一个text文档中添加几条简单的指令而已。
Will My Host Support It? 个人主机支持它吗?
This is probably the hardest question to give a simple answer to.Many hosts support .htaccess but don't actually publicise it andmany other hosts have the capability but do not allow their usersto have a .htaccess file. As a general rule, if your server runsUnix or Linux, or any version of the Apache web server it will support.htaccess, although your host may not allow you to use it.
这可能很难用简单的答案来回答。许多主机支持.htaccess但实际上并不会明确声明,许多其余类型的主机有能力但并不容许他们的用户使用.htaccess文档。做为通常性的规则,若是你的主机使用Unix或Linux系统,或任何版本的Apache网络服务器,它通常来讲是支持.htaccess的,尽管你的主机服务器可能不容许你使用它。
A good sign of whether your host allows .htaccess files is if theysupport password protection of folders. Todo this they will need to offer .htaccess (although in a few casesthey will offer password protection but not let you use .htaccess).The best thing to do if you are unsure is to either upload your own.htaccess file and see if it works or e-mail your web host and askthem.
你的主机是否容许.htaccess,很好的一个标志是它是否支持文件夹的密码保护。为此,他们将提供.htaccess支持(尽管少数状况下他们提供密码保护功能但不容许你使用.htaccess)。若是你不肯定,最好的办法一是上传你本身的.htaccess文档看看是否有用,二是e-mail给你的主机服务商询问。
What Can I Do? 我该怎么作?
You may be wondering what .htaccess can do, or you may have readabout some of its uses but don't realise how many things you canactually do with it.
你可能疑惑.htaccess到底能作些什么,或者你可能曾知道它的一些功能但并不真正了解你实际到底能够用它来作多少事情。
There is a huge range of things .htaccess can do including:password protecting folders, redirecting users automatically,custom error pages, changing your file extensions, banning userswith certian IP addresses, only allowing users with certain IPaddresses, stopping directory listings and using a different fileas the index file.
.htaccess能够作大量范围的事情,包括:文件夹密码保护、用户自动从新指向、自定义错误页面、变动你的文件扩展名、屏蔽特定的用户IP地址、只容许特定的IP地址、中止目录表以及使用其余文件做为index文件。
Creating A .htaccess File 建立一个.htaccess文档
Creating a .htaccess file may cause you a few problems. Writing thefile is easy, you just need enter the appropriate code into a texteditor (like notepad). You may run into problems with saving thefile. Because .htaccess is a strange file name (the file actuallyhas no name but a 8 letter file extension) it may not be acceptedon certain systems (e.g. Windows 3.1). With most operatingsystems, though, all you need to do is to save the file by enteringthe name as:
建立.htaccess文档也许会给你带来几个问题。写文档很容易,你只须要在文字编缉器(例如:写字板)里写下适当的代码。而后,你可能会遇到保存文档的困难,由于.htaccess是一个古怪的文件名(文档事实上没有文件名,只有一个由8个字母组成的扩展名),一些系统(例如windows3.1)没法接受这样的文件名。在大多数的操做系统中,你须要作的是将文档保存成名为:
".htaccess"
(including the quotes). If this doesn't work, you will need to nameit something else (e.g. htaccess.txt) and then upload it to theserver. Once you have uploaded the file you can then rename itusing an FTP program.
(包括引号)。若是这也不行,你须要将其命名为其余的名字(例如htaccess.txt),再将其上传到服务器上,此后你就能够利用FTP软件来重命名它了。
Warning 警告
Before beginning using .htaccess, I should give you one warning.Although using .htaccess on your server is extremely unlikely tocause you any problems (if something is wrong it simply won'twork), you should be wary if you are using the Microsoft FrontPageExtensions. The FrontPage extensions use the .htaccess file so youshould not really edit it to add your own information. If you dowant to (this is not recommended, but possible) you should downloadthe .htaccess file from your server first (if it exists) and thenadd your code to the beginning.
在使用.htaccess以前,我必须给你一些警告。尽管在服务器上使用.htaccess绝对不太可能给你带来任何麻烦(若是有些东西错了,它只是没效用罢了),但若是你使用MicrosoftFrontPage Extensions你就须要当心些。FrontPageExtensions使用了.htaccess,所以你不能编辑它,加入你本身的信息。若是你须要(并不推荐,可是可能)你应该先从服务器上下载.htaccess文档(若是存在),以后在前面加上你的代码。
Custom Error Pages 自定义错误页
The first use of the .htaccess file which I will cover is customerror pages. These will allow you to have your own, personal errorpages (for example when a file is not found) instead of using yourhost's error pages or having no page. This will make your site seemmuch more professional in the unlikely event of an error. It willalso allow you to create scripts to notify you if there is an error(for example I use a PHP script on Free Webmaster Help toautomatically e-mail me when a page is not found).
我想介绍的.htaccess第一个应用是自定义错误页面,这使得你能够拥有本身的、个性化的错误页面(例如找不到文件时),而不是你的服务商提供的错误页或没有任何页面。这会让你的网站在出错的时候看上去更加专业。你还能够利用脚本程序在发生错误的时候通知你(例如我使用FreeWebmaster Help的PHP脚本程序,当找不到页面的时候自动e-mail给我)。
You can use custom error pages for any error as long as you knowits number (like 404 for page not found) by adding the following toyour .htaccess file:
任何你知道代码的错误(像404找不到页面),你均可以将其变成自定义页面,要作的只是在.htaccess文件里加入如下一段:
ErrorDocument errornumber /file.html
For example if I had the file notfound.html in the rootdirect
ory of my site and I wanted to use it for a 404 error I woulduse:
例如,若是个人根目录下有一个nofound.html文档,我想使用它做为404error的页面:
ErrorDocument 404 /notfound.html
If the file is not in the root directory of your site, you justneed to put the path to it:
若是文件不在网站的根目录下,你只须要把路径设置为:
ErrorDocument 500 /errorpages/500.html
These are some of the most common errors:
如下是一些最经常使用的错误:
401 - Authorization Required
400 - Bad request
403 - Forbidden
500 - Internal Server Error
404 - Wrong page
Then, all you need to do is to create a file to display when theerror happens and upload it and the .htaccess file.
这样,你要作的只是生成一个错误显示文档,而后把它们上传。
Part 2 - .htaccess 命令
Introduction 介绍
In the last part I introduced you to .htaccess and some of itsuseful features. In this part I will show you how to use the.htaccess file to implement some of these.
上一部分中我已经将你引入了.htaccess以及它的一些有用的功能,在这一部分里,我将向您演示如何应用.htaccess文档去实现这些功能。
Stop A Directory Index From Being Shown 停示显示目录索引
Sometimes, for one reason or another, you will have no index filein your directory. This will, of course, mean that if someone typesthe directory name into their browser, a full listing of all thefiles in that directory will be shown. This could be a securityrisk for your site.
有些时候,因为某种缘由,你的目录里没有index文件,固然这样意味着若是有人在浏览器地址栏键入了该目录的路径,该目录下全部的文件都会显示出来,这形成了网站的安全威胁。
To prevent against this (without creating lots of new 'index'files, you can enter a command into your .htaccess file to stop thedirectory list from being shown:
为了不这种状况(而没必要建立一堆的新index文档),你能够在你的.htaccess文档中键入如下命令,用以阻止目录索引的显示:
Options -Indexes
Deny/Allow Certian IP Addresses 阻止/容许特定的IP地址
In some situations, you may want to only allow people with specificIP addresses to access your site (for example, only allowing peopleusing a particular ISP to get into a certian directory) or you maywant to ban certian IP addresses (for example, keeping disruptivememembers out of your message boards). Of course, this will onlywork if you know the IP addresses you want to ban and, as mostpeople on the internet now have a dynamic IP address, so this isnot always the best way to limit usage.
有些状况下,你可能只想容许某些特定IP的用户能够访问你的网站(例如:只容许使用特定ISP的用户进入某个目录),或者你想拦截某些特定的IP地址(例如:将低级用户隔离于你的信息面版外)。固然,这只在你知道你想拦截的IP地址时才有用,然而如今网上的大多数用户都使用动态IP地址,因此这并非限制使用的经常使用方法。
You can block an IP address by using:
你可使用如下命令拦截一个IP地址:
deny from 000.000.000.000
where 000.000.000.000 is the IP address. If you only specify 1 or 2of the groups of numbers, you will block a whole range.
被拦截的IP地址则为000.000.000.000,若是你只指定其中1或2个代码组,你能够拦截整个区域的地址。
You can allow an IP address by using:
你可使用如下命令容许一个IP地址的访问:
allow from 000.000.000.000
where 000.000.000.000 is the IP address. If you only specify 1 or 2of the groups of numbers, you will allow a whole range.
被容许的IP地址则为000.000.000.000,若是你只指定其中1或2个代码组,你能够容许整个区域的地址。
If you want to deny everyone from accessing a directory, you canuse:
若是你想阻止全部人访问该文件目录,你可使用:
deny from all
but this will still allow scripts to use the files in thedirectory.
但这将仍然容许脚本程序使用这个目录下的文档。
Alternative Index Files 替代的index文档
You may not always want to use index.htm or index.html as yourindex file for a directory, for example if you are using PHP filesin your site, you may want index.php to be the index file for adirectory. You are not limited to 'index' files though. Using.htaccess you can set foofoo.blah to be your index file if you wantto!
也许你不想一直使用index.htm或index.html来做为目录的索引文档,例如你的站点使用PHP文档,你会想使用index.php来做为该目录的索引文档。固然也没必要局限于“index”文档,若是你愿意,你尽管使用foofoo.balh来做为你的索引文档!
Alternate index files are entered in a list. The server will workfrom left to right, checking to see if each file exists, if none ofthem exisit it will display a directory listing (unless, of course,you have turned this off).
替代的索引文档能够排成一个列表,服务器会从左至右进行寻找,看看哪一个文档在真实的目录中存在。若是一个也找不到,它将会把目录清单显示出来(固然除非你关闭了显示目录文件清单)。
DirectoryIndex index.php index.php3 messagebrd.pl index.htmlindex.htm
Redirection 从新指向
One of the most useful functions of the .htaccess file is toredirect requests to different files, either on the same server, oron a completely different web site. It can be extremely useful ifyou change the name of one of your files but allow users to stillfind it. Another use (which I find very useful) is to redirect to alonger URL, for example in my newsletters I can use a very shortURL for my affiliate links. The following can be done to redirect aspecific file:
.htaccess其中一个极其有用的功能,就是将请求从新指向站内或站外的不一样文档。当你改变了一个文档名称时但仍然想让用户仍然能够用旧连接找到它,这个时候此功能将变得极其有用。另外一个应用(我发现的颇有用的)是从新指向一个长URL,例如在个人时事信息中,我可使用一个很简短的URL来指向个人联合连接。如下是一个从新指向特定文档的例子:
Redirect /location/from/root/file.ext http://www.othersite.com/new/file/location.xyz
In this above example, a file in the root directory calledoldfile.html would be entered as:
上述的例子中,访问在root目录下的名为oldfile.html能够键入:
/oldfile.html
and a file in the old subdirectory would be entered as:
访问一个旧次级目录中的文件能够键入:
/old/oldfile.html
You can also redirect whole directoires of your site using the.htaccess file, for example if you had a directory calledolddirectory on your site and you had set up the same files on anew site at: http://www.newsite.com/newdirectory/ you couldredirect all the files in that directory without having to specifyeach one:
你也可使用.htaccess将整个网站的目录都作从新指向,假如你的网站上有一个名为olddirectory的目录,而且你已经在一个新网站http://www.newsite.com/newdirectory/上创建了与上相同的文档,你能够将旧目录下全部的文件作一次从新指向而没必要一一声明:
Redirect /olddirectory http://www.newsite.com/newdirectory
Then, any request to your site below /olddirectory will beeredirected to the new site, with the
extra information in the URL added on, for example if someone typedin:
这样,任何指向到站点中/olddirectory的请求都将被从新指向新的站点,包括附加的额外URL信息。例若有人键入:
They would be redirected to:
请求将被从新指向到:
This can prove to be extremely powerful if used correctly.
若是正确使用,本功能将极其强大。
Part 3 – 密码保护
Introduction 介绍
Although there are many uses of the .htaccess file, by far the mostpopular, and probably most useful, is being able to relaiblypassword protect directories on websites. Although JavaScript etc.can also be used to do this, only .htaccess has total security (assomeone must know the password to get into the directory, there areno 'back doors')
尽管有各类各样的.htaccess用法,但至今最流行的也多是最有用的作法是将其用于网站目录的可靠的密码保护。尽管JavaScrip等也能作到,但只有.htaccess具备完美的安全(即访问者必须知晓密码才能够访问目录,而且绝无“后门”可走)。
The .htaccess File
Adding password protection to a directory using .htaccess takes twostages. The first part is to add the appropriate lines to your.htaccess file in the directory you would like to protect.Everything below this directory will be password protected:
利用.htaccess将一个目录加上密码保护分两个步骤。第一步是在你的.htaccess文档里加上适当的几行代码,再将.htaccess文档放进你要保护的目录下:
AuthName "Section Name"
AuthType Basic
AuthUserFile /full/path/to/.htpasswd
Require valid-user
There are a few parts of this which you will need to change foryour site. You should replace "Section Name" with the name of thepart of the site you are protecting e.g. "Members Area".
有几个小部分你可能须要根据你的网站状况而修改一下。用被保护部分的名字替换掉”Section Name”,例如"MembersArea"。
The /full/parth/to/.htpasswd should be changed to reflect the fullserver path to the .htpasswd file (more on this later). If you donot know what the full path to your webspace is, contact yoursystem administrator for details.
另外/full/parth/to/.htpasswd应该替换为指向.htpasswd文档(后面详述该文档)的完整服务器路径。若是你不知道你网站空间的完整路径,询问一下你的系统管理员。
The .htpasswd File
Password protecting a directory takes a little more work than anyof the other .htaccess functions because you must also create afile to contain the usernames and passwords which are allowed toaccess the site. These should be placed in a file which (bydefault)should be called .htpasswd. Like the .htaccess file, this is a filewith no name and an 8 letter extension. This can be placed anywherewithin you website (as the passwords are encrypted) but it isadvisable to store it outside the web root so that it is impossibleto access it from the web.
目录的密码保护比.htaccess的其余功能要麻烦些,由于你必须同时建立一个包含用户名和密码的文档,用于访问你的网站,相关信息(默认)应位于一个名为.htpasswd的文档里,像.htaccess同样,.htpasswd也是一个没有文件名且具备8位扩展名的文档,能够放置在你网站里的任何地方(此时密码应加密),但建议你将其保存在网站根目录外,这样经过网络就没法访问到它了。
Entering Usernames And Passwords 输入用户名和密码
Once you have created your .htpasswd file (you can do this in astandard text editor) you must enter the usernames and passwords toaccess the site. They should be entered as follows:
建立好.htpasswd文档后(能够经过文字编辑器建立),下一步是输入用于访问网站的用户名和密码,应为:
username:password
where the password is the encrypted format of the password. Toencrypt the password you will either need to use one of the premadescripts available on the web or write your own. There is a goodusername/password service at the KxS site which will allow you toenter the user name and password and will output it in the correctformat.
“password”的位置应该是加密过的密码。你能够经过几种方法来获得加密过的密码:一是使用一个网上提供的permade脚本或本身写一个;另外一个很不错的username/password加密服务是经过KxS网站,这里容许你输入用户名及密码,而后生成正确格式的密码。
For multiple users, just add extra lines to your .htpasswd file inthe same format as the first. There are even scripts available forfree which will manage the .htpasswd file and will allow automaticadding/removing of users etc.
对于多用户,你只须要在.htpasswd文档中新增一样格式的一行便可。另外还有一些免费的脚本程序能够方便地管理.htpasswd文档,能够自动新增/移除用户等。
Accessing The Site 访问网站
When you try to access a site which has been protected by .htaccessyour browser will pop up a standard username/password dialog box.If you don't like this, there are certain scripts available whichallow you to embed a username/password box in a website to do theauthentication. You can also send the username and password(unencrypted) in the URL as follows:
当你试图访问被.htaccess密码保护的目录时,你的浏览器会弹出标准的username/password对话窗口。若是你不喜欢这种方式,有些脚本程序能够容许你在页面内嵌入username/password输入框来进行认证,你也能够在浏览器的URL框内以如下方式输入用户名和密码(未加密的):
http://username:password@www.website.com/directory/
Summary 小结
.htaccess is one of the most useful files a webmaster can use.There are a wide variety of different uses for it which can savetime and increase security on your website.
.htaccess是一个站点管理员能够应用的一个强大的工具,有更多的变化以适应不一样的用途,能够节约时间及提升网站的安全性。
php
在“经过HTTP状态码查看搜索引擎蜘蛛如何爬行你的网站”一文中,我介绍了一些常常涉及到的HTTP状态码及含义,譬如你们常常探讨而且与本文相关的Http状态码: 404:服务器找不到指定的资源,请求的网页不存在(譬如浏览器请求的网页被删除或者移位,但不排除往后该连接有效的可能性); 410:请求的网页不存在(注意:410表示永久性,而404表示临时性); 200:服务器成功返回请求的网页; 301:网址永久性重定向 302:网址临时性重定向 注意:大部分搜索引擎将“404”与“410”状态同等对待,如Google。(参见Matt Cutts的说明) 对HTTP404状态码的理解 HTTP 404错误意味着连接指向的网页不存在,即原始网页的URL失效,这种状况常常会发生,很难避免,好比说:网页URL生成规则改变、网页文件改名或移动位置、导入连接拼写错误等,致使原来的URL地址没法访问;当Web服务器接到相似请求时,会返回一个404状态码,告诉浏览器要请求的资源并不存在。可是,Web服务器默认的404错误页面,不管Apache仍是IIS,均十分简陋、呆板且对用户不友好,没法给用户提供必要的信息以获取更多线索,无疑这会形成用户的流失。 所以,不少网站均使用自定义404错误的方式以提供用户体验避免用户流失。通常而言,自定义404页面通用的作法是在页面中放置网站快速导航连接、搜索框以及网站提供的特点服务,这样能够有效的帮助用户访问站点并获取须要的信息。 HTTP404对SEO的影响 自定义404错误页面是提供用户体验的很好的作法,但在应用过程当中每每并未注意到对搜索引擎的影响,譬如:错误的服务器端配置致使返回“200”状态码或自定义404错误页面使用MetaRefresh致使返回“302”状态码。正确设置的自定义404错误页面,不只应当可以正确地显示,同时,应该返回“404”错误代码,而不是“200”或“302”。虽然对访问的用户而言,HTTP状态码到底是“404”仍是“200”来讲并无什么区别,但对搜索引擎而言,这则是至关重要的。 (一)自定义404错误页返回“200”状态码 当搜索引擎蜘蛛在请求某个URL时获得“404”状态回应时,即知道该URL已经失效,便再也不索引该网页,并向数据中心反馈将该URL表示的网页从索引数据库中删除,固然,删除过程有可能须要很长时间;而当搜索引擎获得“200”状态回应时,则会认为该url是有效的,便会去索引,并会将其收录到索引数据库,这样的结果即是这两个不一样的url具备彻底相同的内容:自定义404错误页面的内容,这会致使出现复制网页问题。对搜索引擎而言,特别是Google,不但很难得到信任指数TrustRank,也会大大下降Google对网站质量的评定。(为何会出现返回“200”状态码的状况??请参看下面内容“自定义404错误页面的基本原则”) 我一直在使用GoogleSitemap,当咱们提交XML格式网站地图文件时,Google会验证咱们的身份以确保是网站合法的管理者。验证方式有两种:上传指定名称的html页到网站根目录或者在网页meta区域添加一个标识身份的meta标签。我一般是使用上传html网页的方式,但Google却提示我网站根目录下找不到这个网页(实际上我已上传,而且经过浏览器能够访问),这是一个很可怕的问题,见图: (二)自定义404错误页使用Meta Refresh返回“302”状态码 经常看到许多网站的自定义404错误页面采起相似这样的形式:首先显示一段错误信息,而后,经过MetaRefresh将页面跳转到网站首页、网页地图或其余相似页。根据具体实现方式不一样,这类404页面可能返回“200”状态码,也可能返回“302”,但不论哪一种,从SEO技术角度看,均不是一种合适的选择。 对“200”状态的状况咱们上面已经谈过,那么,当404页面返回“302”时,搜索引擎会怎么对待呢?从理论上说,对“302”错误,搜索引擎认为该网页是存在的,只不过临时改变了地址,仍然会索引收录该页,这样,一样会出现相似于“200”状态码时的重复文本问题;其次,以google为表明的主流搜索引擎对302重定向的适用范围要求愈来愈严格,这类不当使用302重定向的状况存在很大的风险。 确保自定义404错误页面可以返回“404”状态码 在自定义404错误页面设置完毕后,必定要检查一下其是否是可以正确地返回“404”状态码。可使用ServerHeader检查工具,输入一个不存在网页的url,查看一下HTTP Header的返回状况,确信其返回的是“404 Notfound”。 404错误的处理方式 (一)定制404错误页面的基本原则 首先应明确的是,404错误应工做在服务器级而不是网页级。对定制使用动态页面如PHP脚本类型的404页时,必须确保在PHP执行前服务器已经顺利地送出“404”状态码,否则,一旦执行到了ISAPI级别,返回的状态码便只能是“200”或其余如“302”之类的重定向状态码了。 其次,在自定义网站的404错误页面时,对设置的错误页面URL连接应使用相对路径而不是绝对路径,并且自定义404页面应该放在网站根目录下。尽管无效连接多是多种形式的URL,但当发生404访问错误时,WEB服务器会自动将其转到自定义的当404错误页中,这跟URL的形似没有关系。 (二)Apache下设置404错误页面 为Apache Server设置 404错误页面的方法很简单,只需在.htaccess 文件中加入以下内容便可: ErrorDocument 404 /notfound.php 注意: 1.切记不要将404错误转向到网站主页,不然可能会致使主页在搜索引擎中消失 2.切记不要使用绝对URL(例如:http://www.bloghuman.com/nofound.php形式),若是使用绝对URL返回的状态码是“302”+“200”(已测试)(三)IIS/ASP.net下设置404错误页面首先,修改应用程序根目录的设置,打开 “web.config” 文件编辑,在其中加入以下内容:<configuration><system.web><customErrors mode=”On” defaultRedirect=”error.asp”><error statusCode=”404″ redirect=”notfound.asp” /></customErrors></system.web></configuration>注:上文例中“error.asp”为系统默认的404页面,“notfound.asp”为自定义的404页面,使用时请修改相应文件名。而后,在自定义的404页面“notfound.asp”中加入:<%Response.Status = “404 Not Found”%>这样,即可以保证IIS可以正确地返回“404”状态码(四)在IIS/ASP.net下设置404静态页面设置静态404错误页面的方法则比较简单,在IIS管理器中右键单击要管理的网站,打开“属性”中的“自定义错误信息”页,为“404”设定相应的错误信息页便可 |