.htaccess 配置文件详解
来源:原创
点击量(289)
发布时间:2013-07-09
<p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;">.htaccess文件设置基础教程 如果你设置好了比如常用的404页面 301重定向 页面还有500页面等会设置了 无非对你的seo技术有很大帮助那么 .htaccess文件到底怎么设置呢 - .htaccess 文件(或者分布式配置</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> .htaccess文件设置基础教程 如果你设置好了比如常用的404页面 301重定向 页面还有500页面等会设置了 无非对你的seo技术有很大帮助那么 .htaccess文件到底怎么设置呢</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> - .htaccess 文件(或者"分布式配置文件")提供了针对目录改变配置的方法, 即,在一个特定的文档目录中放置一个包含一个或多个指令的文件, 以作用于此目录及其所有子目录.[1]作为用户,所能使用的命令受到限制.管理员可以通过Apache的AllowOverride指令来设置.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> - 子目录中的指令会覆盖更高级目录或者主服务器配置文件中的指令.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> - .htaccess必须以ASCII模式上传,最好将其权限设置为644.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 错误文档的定位</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 常用的客户端请求错误返回代码:</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 401 Authorization Required</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 403 Forbidden</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 404 Not Found</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 405 Method Not Allowed</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 408 Request Timed Out</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 411 Content Length Required</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 412 Precondition Failed</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 413 Request Entity Too Long</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 414 Request URI Too Long</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 415 Unsupported Media Type</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 常见的服务器错误返回代码:</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 500 Internal Server Error</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 用户可以利用.htaccess指定自己事先制作好的错误提醒页面.一般情况下,人们可以专门设立一个目录,例如errors放置这些页面.然后再.htaccess中,加入如下的指令:</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> ErrorDocument 404 /errors/notfound.html</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> ErrorDocument 500 /errors/internalerror.html</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 一条指令一行.上述第一条指令的意思是对于404,也就是没有找到所需要的文档的时候得显示页面为/errors目录下的notfound.html页面.不难看出语法格式为:</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> ErrorDocument 错误代码 /目录名/文件名.扩展名</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 如果所需要提示的信息很少的话,不必专门制作页面,直接在指令中使用HTML号了,例如下面这个例子:</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> ErrorDocument 401 "</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 你没有权限访问该页面,请放弃!</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> "</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 文档访问的密码保护</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 要利用.htaccess对某个目录下的文档设定访问用户和对应的密码,首先要做的是生成一个.htpasswd的文本文档,例如:</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> zheng:y4E7Ep8e7EYV</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 这里密码经过加密,用户可以自己找些工具将密码加密成.htaccess支持的编码.该文档最好不要放在www目录下,建议放在www根目录文档之外,这样更为安全些.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 有了授权用户文档,可以在.htaccess中加入如下指令了:</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> AuthUserFile .htpasswd的服务器目录</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> AuthGroupFile /dev/null (需要授权访问的目录)</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> AuthName EnterPassword</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> AuthType Basic (授权类型)</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> require user wsabstract (允许访问的用户,如果希望表中所有用户都允许,可以使用 require valid-user)</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 注,括号部分为学习时候自己添加的注释</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 拒绝来自某个IP的访问</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 如果我不想某个政府部门访问到我的站点的内容,那可以通过.htaccess中加入该部门的IP而将它们拒绝在外.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 例如:</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> order allow,deny</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> deny from 210.10.56.32</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> deny from 219.5.45.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> allow from all</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> (2)</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 保护.htaccess文档</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 在使用.htaccess来设置目录的密码保护时,它包含了密码文件的路径.从安全考虑,有必要把.htaccess也保护起来,不让别人看到其中的内容.虽然可以用其他方式做到这点,比如文档的权限.不过,.htaccess本身也能做到,只需加入如下的指令:</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> order allow,deny</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> deny from all</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> URL转向</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 我们可能对网站进行重新规划,将文档进行了迁移,或者更改了目录.这时候,来自搜索引擎或者其他网站链接过来的访问就可能出错.这种情况下,可以通过如下指令来完成旧的URL自动转向到新的地址:</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> Redirect /旧目录/旧文档名 新文档的地址</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 或者整个目录的转向:</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> Redirect 旧目录 新目录</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 改变缺省的首页文件</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 一般情况下缺省的首页文件名有default、index等.不过,有些时候目录中没有缺省文件,而是某个特定的文件名,比如在pmwiki中是pmwiki.php.这种情况下,要用户记住文件名来访问很麻烦.在.htaccess中可以轻易的设置新的缺省文件名:</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> DirectoryIndex 新的缺省文件名</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 也可以列出多个,顺序表明它们之间的优先级别,例如:</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> DirectoryIndex filename.html index.cgi index.pl default.htm</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 防止盗链</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 如果不喜欢别人在他们的网页上连接自己的图片、文档的话,也可以通过htaccess的指令来做到.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 所需要的指令如下:</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> RewriteEngine on</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> RewriteCond \%{HTTP_REFERER} !^$</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> RewriteCond \%{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC]</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> RewriteRule \.(gif|jpg)$ - [F]</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 如果觉得让别人的页面开个天窗不好看,那可以用一张图片来代替:</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> RewriteEngine on</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> RewriteCond \%{HTTP_REFERER} !^$</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> RewriteCond \%{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC]</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> RewriteRule \.(gif|jpg)$ http://www.mydomain.com/替代图片文件名 [R,L]</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> (3)</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> .htaccess is a very useful way to accomplish things that HTML and CSS can't, however it is very rarely allowed on free-servers. You should check with your system administrator to make sure that .htaccess is allowed/enabled before trying out these tutorials.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 1.error pages: customize your 404 error pages.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> We all know what a 404 "Not Found" error page looks like. This tutorial will show you how to make those error pages anything you desire.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> First you'll need to create your error pages. Just make some normal HTML pages, one for each error type. The error types and their definitions are:</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 400: Bad Request. The server doesn't understand the request.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 401: Authentication Failed. The password was not accepted.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 403: Access Forbidden. Access to the area is forbidden.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 404: File Not Found. The requested file could not be found.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 500: Internal Server Error. Usually the result of a misconfigured script.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> Once you've made your error pages, upload them to a directory called error or something of the sort.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> Now the important part. Create a file called htaccess.txt in Notepad. Add these lines to the file:</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> ErrorDocument 400 http://iiwnet.com/error/400.html</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> ErrorDocument 401 http://iiwnet.com/error/401.html</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> ErrorDocument 403 http://iiwnet.com/error/403.html</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> ErrorDocument 404 http://iiwnet.com/error/404.html</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> ErrorDocument 500 http://iiwnet.com/error/500.html</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> Change iiwnet.com to the location of your website. Save the file and upload it to your top-most web directory. Once it's there, rename it with your FTP program to .htaccess - complete with the dot starting it out, and with no .txt extension this time.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> If the .htaccess file is in your top-most web directory, then all errors that occur in that directory and all its subdirectories will be referred to the appropriate error page.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> That's it! Try it out by going to a page on your website that you know doesn't exist.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 2.private directories: password protect your private folders.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> Ever wanted a password protected directory? Click here for an example, use kali as your username and green as the password. With .htaccess and .htpasswd you can create as many users and passwords as you need.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> To have one of your own, download this cgi script: password.cgi.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> To use it, unzip the script and upload it to your cgi-bin.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> CHMOD it 755 and run it from your web-browser.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> Instructions will be detailed in the script.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> 3.anti-leech: stop people from linking to your image files.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> If you've ever gone through your site logs and found someone hotlinking to your image files, stealing your bandwidth, then you need the following lines in your .htaccess file:</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> RewriteEngine on</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> RewriteCond \%{HTTP_REFERER} !^$</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> RewriteCond \%{HTTP_REFERER} !^http://iiwnet.com/.*$ [NC]</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> RewriteCond \%{HTTP_REFERER} !^http://www.iiwnet.com/.*$ [NC]</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> RewriteRule .*\.(gif|GIF|jpg|JPG|bmp|BMP)$ - [F]</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> Change "iiwnet.com" to your own URL, and upload the .htaccess file to the directory containing your images. Once it is in place, only people coming from "iiwnet.com" will be able to view the images; everyone else will only see a broken image placeholder. If you have an image you'd like to display instead of the placeholder, use the version below.</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> RewriteEngine On</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> RewriteCond \%{HTTP_REFERER} !^$</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> RewriteCond \%{HTTP_REFERER} !^http://iiwnet.com/ [NC]</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> RewriteCond \%{HTTP_REFERER} !^http://www.iiwnet.com/ [NC]</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> RewriteCond \%{REQUEST_URI} !^/theif.gif [NC]</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> RewriteRule \.(gif|GIF|jpg|JPG)$ [R]</p><p style="margin:0px auto;padding:0px;line-height:25px;color:#666666;width:665px;font-size:14px;"> "theif.gif" is the image you want to load whenever someone attempts to hotlink an image from your server. You only need to add the RewriteCond \%{REQUEST_URI} !^/theif.gif [NC] line if the image you want to load is in a directory that the .htaccess file effects. Otherwise you can remove the line, and it will still function.</p><p><br /></p>