ID:   密码:   验证码:  
我的知识库
服务器-WEB服务器
服务器-FTP服务器
服务器-安全防护
IDC业务相关
网络编程-ASP
网络编程-PHP
网络编程-.NET
网络编程-XML
数据库-Access
数据库-Mssql
数据库-Mysql
主机相关问题
域名相关问题
邮局相关问题
付款相关问题
相关文本合同
游戏技术-传奇私服架设
游戏技术-传奇脚本信息
游戏技术-传世私服
常用软件下载
首页 >> 我的知识库>>服务器-WEB服务器>> htaccess介绍和使用方法  
htaccess介绍和使用方法
[ 作者:  | 文章来源: 服务器技术 | 点击数: 613 | 更新时间: 2007-10-13 17:36:24 ]

.htaccess文件是非常有用的,下面一篇介绍:

中.国.南通服务器网

• Part 1 – Introduction介绍 南通服务器网

• Part 2 - .htaccess Commande命令 Spdns

• Part 3 - Password protection密码保护

中国南通服务器网

Part 1 – Introduction介绍

中.国.南通服务器网

Introduction 介绍 中国.南通服务器网

In this tutorial you will find out about the .htaccess file and the power it has to improve your website. Although .htaccess is only a file, it can change settings on the servers and allow you to do many different things, the most popular being able to have your own custom 404 error pages. .htaccess isn't difficult to use and is really just made up of a few simple instructions in a text file. Spdns@com

从本指南中,您可以学习到有关.htaccess文档及其功能,用以优化您的网南通服务器网

Will My Host Support It? 我的主机支持它吗?

Spdns@com

This is probably the hardest question to give a simple answer to. Many hosts support .htaccess but don't actually publicise it and many other hosts have the capability but do not allow their users to have a .htaccess file. As a general rule, if your server runs Unix or Linux, or any version of the Apache web server it will support .htaccess, although your host may not allow you to use it.

Www^Spdns^com

这可能很难用简单的答案来回答。许多主机支持.htaccess但实际上并不会明确声明,许多其他类型的主机有能力但并不允许他们的用户使用.htaccess文档。作为一般性的规则,如果你的主机使用Unix或Linux系统,或任何版本的Apache网络服务器,它一般来说是支持.htaccess的,尽管你的主机服务器可能不允许你使用它。 Spdns

A good sign of whether your host allows .htaccess files is if they support password protection of folders. To do this they will need to offer .htaccess (although in a few cases they 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 ask them.

南通服务器网

你的主机是否允许.htaccess,很好的一个标志是它是否支持文件夹的密码保护。为此,他们将提供.htaccess支持(尽管少数情况下他们提供密码保护功能但不允许你使用.htaccess)。如果你不确定,最好的办法一是上传你自己的.htaccess文档看看是否有用,二是e-mail给你的主机服务商询问。 中国南通服务器网

What Can I Do? 我该怎么做?

Spdns@com

You may be wondering what .htaccess can do, or you may have read about some of its uses but don't realise how many things you can actually do with it. Spdns

你可能疑惑.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 users with certian IP addresses, only allowing users with certain IP addresses, stopping directory listings and using a different file as the index file. 中.国南通服务器网

.htaccess可以做大量范围的事情,包括:文件夹密码保护、用户自动重新指向、自定义错误页面、变更你的文件扩展名、屏蔽特定的用户IP地址、只允许特定的IP地址、停止目录表以及使用其他文件作为index文件。 Spdns@com

Creating A .htaccess File 创建一个.htaccess文档

中国.南通服务器网

Creating a .htaccess file may cause you a few problems. Writing the file is easy, you just need enter the appropriate code into a text editor (like notepad). You may run into problems with saving the file. Because .htaccess is a strange file name (the file actually has no name but a 8 letter file extension) it may not be accepted on certain systems (e.g. Windows 3.1). With most operating systems, though, all you need to do is to save the file by entering the name as: Spdns^com

创建.htaccess文档也许会给你带来几个问题。写文档很容易,你只需要在文字编缉器(例如:写字板)里写下适当的代码。然后,你可能会遇到保存文档的困难,因为.htaccess是一个古怪的文件名(文档事实上没有文件名,只有一个由8个字母组成的扩展名),一些系统(例如windows 3.1)无法接受这样的文件名。在大多数的操作系统中,你需要做的是将文档保存成名为:

中.国.南通服务器网

".htaccess"
(including the quotes). If this doesn't work, you will need to name it something else (e.g. htaccess.txt) and then upload it to the server. Once you have uploaded the file you can then rename it using an FTP program. 中国南通服务器网

(包括引号)。如果这也不行,你需要将其命名为其他的名字(例如htaccess.txt),再将其上传到服务器上,此后你就可以利用FTP软件来重命名它了。

中.国南通服务器网

Warning 警告 Spdns@com

Before beginning using .htaccess, I should give you one warning. Although using .htaccess on your server is extremely unlikely to cause you any problems (if something is wrong it simply won't work), you should be wary if you are using the Microsoft FrontPage Extensions. The FrontPage extensions use the .htaccess file so you should not really edit it to add your own information. If you do want to (this is not recommended, but possible) you should download the .htaccess file from your server first (if it exists) and then add your code to the beginning.

南通服务器网

在使用.htaccess之前,我必须给你一些警告。尽管在服务器上使用.htaccess绝对不太可能给你带来任何麻烦(如果有些东西错了,它只是没效用罢了),但如果你使用Microsoft FrontPage Extensions你就需要小心些。FrontPage Extensions使用了.htaccess,因此你不能编辑它,加入你自己的信息。如果你需要(并不推荐,但是可能)你应该先从服务器上下载.htaccess文档(如果存在),之后在前面加上你的代码。

中.国.南通服务器网

Custom Error Pages 自定义错误页 Spdns_com

The first use of the .htaccess file which I will cover is custom error pages. These will allow you to have your own, personal error pages (for example when a file is not found) instead of using your host's error pages or having no page. This will make your site seem much more professional in the unlikely event of an error. It will also allow you to create scripts to notify you if there is an error (for example I use a PHP script on Free Webmaster Help to automatically e-mail me when a page is not found). 中.国.南通服务器网

我想介绍的.htaccess第一个应用是自定义错误页面,这使得你可以拥有自己的、个性化的错误页面(例如找不到文件时),而不是你的服务商提供的错误页或没有任何页面。这会让你的网南通服务器网

ErrorDocument 404 /notfound.html
If the file is not in the root directory of your site, you just need to put the path to it:

中.国.南通服务器网

如果文件不在网南通服务器网

这样,你要做的只是生成一个错误显示文档,然后把它们上传。

南通服务器网


Part 2 - .htaccess 命令 [南通服务器网]

Introduction 介绍

Www.Spdns.com

In the last part I introduced you to .htaccess and some of its useful features. In this part I will show you how to use the .htaccess file to implement some of these. Spdns

上一部分中我已经将你引入了.htaccess以及它的一些有用的功能,在这一部分里,我将向您演示如何应用.htaccess文档去实现这些功能。 [南通服务器网]

Stop A Directory Index From Being Shown 停示显示目录索引 南通服务器网

Sometimes, for one reason or another, you will have no index file in your directory. This will, of course, mean that if someone types the directory name into their browser, a full listing of all the files in that directory will be shown. This could be a security risk for your site.

中国南通服务器网

有些时候,由于某种原因,你的目录里没有index文件,当然这样意味着如果有人在浏览器地址栏键入了该目录的路径,该目录下所有的文件都会显示出来,这造成了网南通服务器网

为了避免这种情况(而不必创建一堆的新index文档),你可以在你的.htaccess文档中键入以下命令,用以阻止目录索引的显示: Spdns.com

Options -Indexes
Deny/Allow Certian IP Addresses 阻止/允许特定的IP地址 南通服务器网

In some situations, you may want to only allow people with specific IP addresses to access your site (for example, only allowing people using a particular ISP to get into a certian directory) or you may want to ban certian IP addresses (for example, keeping disruptive memembers out of your message boards). Of course, this will only work if you know the IP addresses you want to ban and, as most people on the internet now have a dynamic IP address, so this is not always the best way to limit usage. Www.Spdns.com

有些情况下,你可能只想允许某些特定IP的用户可以访问你的网南通服务器网

You can block an IP address by using: Spdns.com

你可以使用以下命令拦截一个IP地址: 南通服务器网

deny from 000.000.000.000
where 000.000.000.000 is the IP address. If you only specify 1 or 2 of 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 2 of the groups of numbers, you will allow a whole range.

中国南通服务器网,为中文网南通服务器网

如果你想阻止所有人访问该文件目录,你可以使用: 中.国南通服务器网

deny from all
but this will still allow scripts to use the files in the directory. 中.国南通服务器网

但这将仍然允许脚本程序使用这个目录下的文档。

南通服务器网

Alternative Index Files 替代的index文档 Spdns.com

You may not always want to use index.htm or index.html as your index file for a directory, for example if you are using PHP files in your site, you may want index.php to be the index file for a directory. You are not limited to 'index' files though. Using .htaccess you can set foofoo.blah to be your index file if you want to!

南通服务器网

也许你不想一直使用index.htm或index.html来作为目录的索引文档,例如你的南通服务器网

Alternate index files are entered in a list. The server will work from left to right, checking to see if each file exists, if none of them exisit it will display a directory listing (unless, of course, you have turned this off).

Www_Spdns_com

替代的索引文档可以排成一个列表,服务器会从左至右进行寻找,看看哪个文档在真实的目录中存在。如果一个也找不到,它将会把目录清单显示出来(当然除非你关闭了显示目录文件清单)。

南通服务器网

DirectoryIndex index.php index.php3 messagebrd.pl index.html index.htm
Redirection 重新指向 中国南通服务器网,为中文网南通服务器网.长.南通服务器网

/old/oldfile.html
You can also redirect whole directoires of your site using the .htaccess file, for example if you had a directory called olddirectory on your site and you had set up the same files on a new site at: http://www.newsite.com/newdirectory/ you could redirect all the files in that directory without having to specify each one: Www@Spdns@com

你也可以使用.htaccess将整个网南通服务器网

http://www.newsite.com/newdirectory/oldfiles/images/image.gif
This can prove to be extremely powerful if used correctly.

南通服务器网

如果正确使用,本功能将极其强大。

Spdns^com

Part 3 – 密码保护 Spdns~com

Introduction 介绍 Spdns

Although there are many uses of the .htaccess file, by far the most popular, and probably most useful, is being able to relaibly password protect directories on websites. Although JavaScript etc. can also be used to do this, only .htaccess has total security (as someone must know the password to get into the directory, there are no 'back doors') Spdns^com

尽管有各种各样的.htaccess用法,但至今最流行的也可能是最有用的做法是将其用于网南通服务器网

The .htaccess File Spdns_com

Adding password protection to a directory using .htaccess takes two stages. 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 for your site. You should replace "Section Name" with the name of the part of the site you are protecting e.g. "Members Area".

Spdns@com

有几个小部分你可能需要根据你的网南通服务器网

另外/full/parth/to/.htpasswd 应该替换为指向.htpasswd文档(后面详述该文档)的完整服务器路径。如果你不知道你网南通服务器网

The .htpasswd File 南通服务器网

Password protecting a directory takes a little more work than any of the other .htaccess functions because you must also create a file to contain the usernames and passwords which are allowed to access the site. These should be placed in a file which (by default) should be called .htpasswd. Like the .htaccess file, this is a file with no name and an 8 letter extension. This can be placed anywhere within you website (as the passwords are encrypted) but it is advisable to store it outside the web root so that it is impossible to access it from the web. 中国南通服务器网

目录的密码保护比.htaccess的其他功能要麻烦些,因为你必须同时创建一个包含用户名和密码的文档,用于访问你的网南通服务器网

Once you have created your .htpasswd file (you can do this in a standard text editor) you must enter the usernames and passwords to access the site. They should be entered as follows:

南通服务器网

创建好.htpasswd文档后(可以通过文字编辑器创建),下一步是输入用于访问网南通服务器网

username:password
where the password is the encrypted format of the password. To encrypt the password you will either need to use one of the premade scripts available on the web or write your own. There is a good username/password service at the KxS site which will allow you to enter the user name and password and will output it in the correct format. 中国南通服务器网,为中文网南通服务器网

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 save time and increase security on your website.

Www~Spdns~com

.htaccess是一个南通服务器网,为中文网站提供动力

Tags:htaccess 介绍 使用方法
责任编辑:阿小



评论
收藏
推荐
打印
关闭
字体:+ -
纠错


Copyright © 1999-2007 诚信 合法 规范的南通服务器网 www.spdns.com 始建于1996
企业总机:400-618-5529(只收市话费) 未开通400地区:0513-83015999 / 84012988 / 84519829
上海亿流数码科技有限公司 地址:江苏省南通市如东电信局大楼3楼IDC运维部
《中华人民共和国增值电信业务经营许可证》沪B2-20060518号
信息产业部备案编号:苏ICP备05009350号
电子执照:沪企3102272090823