欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

16个简单实用的.htaccess使用技巧

程序员文章站 2023-02-18 22:29:28
.htaccess 文件 (hypertext access file) 是apache web服务器的一个非常强大的配置文件,对于这个文件,apache有一堆参数可以让你...

.htaccess 文件 (hypertext access file) 是apache web服务器的一个非常强大的配置文件,对于这个文件,apache有一堆参数可以让你配置出几乎随心所欲的功能。.htaccess 配置文件坚持了unix的一个文化——使用一个ascii 的纯文本文件来配置你的网站的访问策略。

这篇文章包括了16个非常有用的小技巧。另外,因为.htaccess 是一个相当强大的配置文件,所以,一个轻微的语法错误会造成你整个网站的故障,所以,在你修改或是替换原有的文件时,一定要备份旧的文件,以便出现问题的时候可以方便的恢复。

1. 使用.htaccess 创建自定义的出错页面。对于linux apache来说这是一项极其简单的事情。使用下面的.htaccess语法你可以轻松的完成这一功能。(把.htaccess放在你的网站根目录下)

errordocument 401 /error/401.php
errordocument 403 /error/403.php
errordocument 404 /error/404.php
errordocument 500 /error/500.php

2. 设置网站的时区

setenv tz america/houston

3. 阻止ip列表
有些时候,你需要以ip地址的方式阻止一些访问。无论是对于一个ip地址还是一个网段,这都是一件非常简单的事情,如下所示:

allow from all
deny from 145.186.14.122
deny from 124.15

apache对于被拒绝的ip会返回403错误。

4. 把一些老的链接转到新的链接上——搜索引擎优化seo

redirect 301 /d/file.html http://www.htaccesselite.com/r/file.html

5. 为服务器管理员设置电子邮件。

serversignature email
setenv server_admin default@domain.com

6. 使用.htaccess 访止盗链。如果你网站上的一个图片被别的n多的网站引用了,那么,这很有可能会导致你服务器的性能下降,使用下面的代码可以保护某些热门的链接不被过多的引用。

options +followsymlinks
# protect hotlinking
rewriteengine on
rewritecond %{http_referer} !^$
rewritecond %{http_referer} !^http://(www.)?domainname.com/ [nc]
rewriterule .*.(gif|jpg|png)$ http://domainname.com/img/hotlink_f_o.png [nc]

7. 阻止 user agent 的所有请求

## .htaccess code :: begin
## block bad bots by user-agent
setenvifnocase user-agent ^frontpage [nc,or]
setenvifnocase user-agent ^java.* [nc,or]
setenvifnocase user-agent ^microsoft.url [nc,or]
setenvifnocase user-agent ^msfrontpage [nc,or]
setenvifnocase user-agent ^offline.explorer [nc,or]
setenvifnocase user-agent ^[ww]eb[bb]andit [nc,or]
setenvifnocase user-agent ^zeus [nc]

order allow,deny
allow from all
deny from env=bad_bot

## .htaccess code :: end

8. 把某些特殊的ip地址的请求重定向到别的站点

errordocument 403 http://www.youdomain.com
order deny,allow
deny from all
allow from ip
allow from ip

9. 直接找开文件而不是下载 – 通常,我们打开网上文件的时候总是会出现一个对话框问我们是下载还是直接打开,使用下面的设置就不会出现这个问题了,直接打开

addtype application/octet-stream .pdf
addtype application/octet-stream .zip
addtype application/octet-stream .mov

10. 修改文件类型 – 下面的示例可以让任何的文件都成为php那么被服务器解释。比如:myphp, cgi,phtml等。

forcetype application/x-httpd-php
sethandler application/x-httpd-php

11. 阻止存取.htaccess 文件

# secure htaccess file
order allow,deny
deny from all

12. 保护服务器上的文件被存取

# prevent access of a certain file order allow,deny
deny from all

13. 阻止目录浏览

# disable directory browsing
options all -indexes

14. 设置默认主页

# serve alternate default index page
directoryindex about.html

15. 口令认证 – 你可以创建一个文件用于认证。下面是一个示例:

# to protect a file

authtype basic
authname “prompt”
authuserfile /home/path/.htpasswd
require valid-user

# password-protect a directory
resides
authtype basic
authname “this directory is protected”
authuserfile /home/path/.htpasswd
authgroupfile /dev/null
require valid-user

16. 把老的域名转像新的域名

# redirect from old domain to new domain
rewriteengine on
rewriterule ^(.*)$ http://www.yourdomain.com/$1 [r=301,l]