当前位置: 首页 > 网络学院 > 设计教程 > 设计理念 > Htaccess简介
What is ‘htaccess’ you may ask yourself. “What the heck, I don’t need that rubbish!” Well, let me tell you one thing, it’s very useful and you’ll be appreciated when you run into htaccess problems. Htaccess has the power to improve you’re website a lot, for an example:
你可能会问自己,到底什么是“htaccess”?“是黑客程序吗?我不需要这样的垃圾软件”。好吧,让我来告诉你它是什么吧,当你碰到与“htaccess”相关的问题时,你就会觉得“htaccess”很有用了,并会很感激它给你带来的帮助。“Htaccess”可以帮助你大幅度地改进你的网站,举个例子:
change settings on you’re server, design you’re own custom 404 error pages. Htaccess isn’t that difficult to use, and it’s really simple to learn.
改变你的服务器属性,对你的404错误页面加以设计。Htaccess在使用上并不复杂,相反,它很容易掌握。
Webspace and support
网络空间和支持
Some hosts do support. Htaccess but doesn’t publicise it, for security reasons. If you’re on a host that have Unix/Linux installed, or runs any Apache web server it will support htaccess. Another sign, to tell if you’re host supports .htaccess. If you’re having the option to create password protected directories, they’ll need to offer you .htaccess, although most hosts don’t.
许多虚拟主机都支持它。然而,考虑到安全因素,不要宣称使用了它。如果你的虚拟主机装有Unix/Linux系统,或运行着Apache(阿帕奇)服务器,那么,这些系统都支持“.htaccess”;另一方面,虚拟主机提供上需要告诉你这些内容:你的虚拟主机是否支持“.htaccess”,或者,你是否有权利创建密码来保护你的地址名录。他们应该为你提供对“.htaccess”的支持,尽管大多数虚拟主机并未提供这些内容。
Try upload a .htaccess file to you’re host, or try to contact their support center for more information.
尝试一下将“.htaccess”上传到你的虚拟主机,或者,尝试着联系虚拟主机服务中心,以获取更多的信息。
Uses of htaccess
htaccess的使用
You might have heard some of these functions:
你可能听说过下面这些功能:
Password protecting, redirect users automatically, error pages, change file extensions, block certain IP addresses, or allows certain sessions to view certain pages.
密码保护,自动跳转页面,找不到页面,改变文件的扩展名,过滤指定的IP地址,允许指定的绘画程序访问指定页面。
There are many functions, not half of them is mentioned above.
这里有很多功能,上述的功能仅仅是皮毛而已。
Writing a htaccess file
书写一个”.htaccess”文件
Writing a htaccess file is easy, but you could be stuck with problems, with the file. Write the appropriated code into a text editor, such as notepad. Saving the file on operating systems like Windows 3.1. Most of the systems, you just need to save it like this:
书写一个“.htaccess”文件很容易,但是你要学会面对所有的问题以及文件。你可以使用文本编辑器来编写你需要的代码,如“记事本”,并向Windows 3.1 这样的操作系统一样来存储文件。对于大多数文件,你需要使用下面的代码来存储:
CODE
代码
“.htaccess”
Include the quotes, for special saving as another file then .txt. If that doesn’t work, name it whatever you want, without the quotes. I used to change my files with a smart ftp program!
包括引号,如同其它的.txt文件一样来存储。如果它不能运行,你可以对它进行重命名,不需要写上引号。以前,我通常使用smart ftp程序来更改我的文件!
404: Error Pages
找不到页面
My first lesson is to teach you how to create a own custom-made error page. This will make you’re page look much more professional and dynamical. To write a custom error page, write this text code:
我的第一课就是教你如何创建属于你自己的“custom-made error page(客户定制的404错误页面)”,它可以使你的页面看上去更加专业,并且更具备动态性。书写一份“custom error page(客户端错误页面)”需要使用下面的代码:
CODE
代码
ErrorDocument theerrornumber/thefile.php
(You can use whatever you want. [你可以写上你自己想要写的内容] )
For a real example, I’ll write my own custom-made error page.
对于一个真实的例子来说,我将书写属于自己的“custom-made error page(客户定制的404错误页面)”
CODE
代码
ErrorDocument 404 /nomatch.php
You can easily configure the path of you’re file, but simply write “ErrorDocument 404/notfound/nomatch.php”.
你可以很轻松地设定文件的路径,而仅仅只要书写“ErrorDocument 404/notfound/nomatch.php”就可以了。
I’ll write the most common error pages, in the web.
我将列举一些网络中最常用的错误页代码。
CODE
代码
401 – Authorisation required 需要验证
500 – Internal Server Error 内部服务器错误
404 – Wrong page 找不到页面
400 – Bad request 错误请求
403 – Forbidden 禁止
After that, just make you’re own custom-made error page, and upload you’re .htaccess file.
然后,制作好你的“custom-made error page(客户定制的404错误页面)”,然后上传你的“.htaccess”文件。
Let's take a look at some functions and commands, that a .htaccess file can be used for.
让我们看看“htaccess”文件的一些相关功能和命令。
Useful stuffs
有用的原料
Index file for diffrent directories.
关于不同地址名录的索引文件
This is pretty useful if you want diffrent index files for diffrent directories. When you're using PHP, this is very useful.
如果你想对不同的地址名录设定不同的索引文件,那么这将非常有用。当你正在使用PHP时,它就显得非常有用。
For a example you could write yourock.php as you're directory index file.
举个例子来说,你可以书写一个“vourock.php”文件作为你的索引文件。
CODE
代码
DirectoryIndex index.jsp index.asp index.aspx index.html index.htm
目录索引文件:index.jsp index.asp index.aspx index.html index.htm
That's just a example, but I suggest you fill them with you're own files. It will show the root directory (if none file was found) in a list.
这仅仅是一个例子,但是我还是希望你能够使用自己的文件来填充它们。它将在列表中显示根目录(如果没有找到任何文件)。
Stop directory index for showing.
关闭目录索引并展示
If you're not using a index or main file, all you're directories will be shown in the root directory. This is a security risk, so for preventing this we could write in our .htaccess file:
如果你并没有使用索引文件或主文件,那么,所有你的目录将会在根目录中显示出来。这里存在着一个安全隐患。因此,为了避免这一点,我们通常会写上一个“.htaccess”文件。
CODE
代码
Options –Indexes
选项 – 索引
Okay, now we've done that I should introduce you to some other functions that htacess has to offer (niffy file, ain't it?)
好吧,完成了上述的事项之后,我可以向你介绍一些“htaccess”的功能了(niffy文件,不是吗?)
I was thinking about transfer sessions and users, to special files or sites. With this cool thing you could move me to another site or file, even another HTTP host. I'll explain more beneath.
我正在思考关于向特定的文件或网站中传输会话程序和用户的问题。通过实现这样的功能,你就可以把我移动到另一个网站或文件中,甚至是另一个HTTP主机,我将在下面所详细叙述。
Transfer users
转移用户
If you've change a file, or just moved you're site to another host and still have the old host active - this is very useful for you to keep you're visitors alert and to keep coming back to you're site. This could be used with
如果你已经改变了一个文件,或者,刚把你的网站转移到另一个虚拟主机上,并且,你原有的主机仍然处于激活状态,这是,就很有必要提醒你的访问者去访问新的网络地址。这个功能同样也可以在META 标签中实现,但是这个“.htaccess”文件证明你也可以使用这个方法来实现它。
CODE
代码
redirect folderfilename.html http://thesite.com
The above is just a example, you could enter another site and filename.
上述只是一个例子,同样地,你可以输入另一个网址或文件名。
Another function, same as the redirection is that you could move a entire folder of you're choice, if you wanted.
与页面转向类似的另一个功能就是:你可以移动你所选择的整个文件夹。
If you choose a directory, it will move all the files that are in that folder - even other directories.
你如你选择了一个目录,它将移动这个目录下所有的文件和文件夹。
CODE
代码
redirect /directory http://thesite.com/directory/
Auth a session/user
Okay, this niffy little trick could protect special sites or directories to be listed if you do it the right way. This is a password protection, easy to handle. It may keep users from visiting the files that are password protected.
OK,这可以保护你所指定的网站和目录,只有在你授权的情况下才会以列表的形式显示。这是一个密码保护程序,很容易操作。它可以使得用户不能对加密的文件进行访问。
We'll begin with creating a session & password. We'll require that you create a new htaccess file, 8 characters long file extension. I suggest you open Notepad for this, easy to use. Just write these text in the new password htaccess file:
下面,我们将开始学习创建会话程序和加密程序。我要求你需要创建一个新的“htaccess”文件,包含8个字符长度的文件扩展名。我建议你打开记事本来完成这个工作,它使用起来很方便,只要在上面书写下面这段文字就可以了。
CODE
代码
username:password
I suggest you encrypt the password with the password generator. There you are adviced to input you're password and username. When you've got the result, paste it into the htaccess password file with the username / password.
我建议你使用password generator[密码生成器]对密码进行加密。我们建议你在这种情况下来输入你的用户名和密码。当你得到一个结果后,你可以将这个结果连同你的用户名和密码一起粘贴到“htaccess” 密码文件中。
sdsdsd:vJU6uv9VRuH3w
Like that, a example. Save the password file, you can save it wherever you want. It's encrypted, you could put it outside you're root directory for 100% security. So no one can access it from the web. Now we're gonna access the site, and "log in" with our authname and password.
就像上面那样,保存密码文件,你可以保存所有你想保存的东西。它们都是加密的,你可以百分之一百安全地将它们放置在根目录之外。因为,没有人可以通过网络来访问到它。现在,我们可以再次访问网站,并登陆authname[授权用户名]和password[密码]。
Remember the username (not encrypted) and password (also not encrypted).
记住用户名(未被加密的)和密码(未被加密的)
http://username:[email protected]/directory/file