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

.htaccess文件使用教程总结

程序员文章站 2022-04-28 15:56:23
1. apache中启用.htaccess 修改 /private/etc/apache2/httpd.conf options followsymlinks...

1. apache中启用.htaccess

修改 /private/etc/apache2/httpd.conf

options followsymlinks
# allowoverride none
allowoverride all

# 去掉注释
loadmodule rewrite_module libexec/apache2/mod_rewrite.so

# 可以选择修改使用.htaccess以外的文件名,如.config
accessfilename .config

2. 时区设置

setenv tz asia/shanghai

3. 显示/隐藏目录列表

# 允许显示,两种方式
options indexes followsymlinks
options all +indexes
# 隐藏目录,三种方式
options followsymlinks
options all -indexes
options -indexes

4. 访问控制

使用order命令限制用户访问一些关键目录

# 保护 htaccess 文件
<files .htaccess>
order allow,deny
deny from all
</files>

# 阻止查看所有文件
<files *>
order allow,deny
deny from all
</files>

# 阻止查看指定的文件
<files logo.png>
order allow,deny
deny from all
</files>

# 多种文件类型
<filesmatch ".(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$">
order allow,deny
deny from all
</filesmatch>

5. 重定向

redirect permanent / //www.jb51.net
redirect temp /old.html //www.jb51.net/index.html
order deny,allow

6. url重写

# 开启url重写
rewriteengine on
# 重写规则
rewriterule ^demo/getnew/([0-9]+)$ index.php/demo/getnew?id=$1
rewriterule ^demo/(\s+)$ index.php/demo/$1

7. 阻止/允许特定ip

order allow,deny
deny from 123.123.123.123
deny from 123.123.1
allow from all

8. 自定义错误页

# 基于网站根目录
errordocument 404 /pages/404.html
errordocument 500 /pages/500.html

9. 缺省页

directoryindex index.html index.htm index.php

10. 使用/禁用缓存文件

<filesmatch ".(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$">
header set cache-control "max-age=2592000"
</filesmatch>
<filesmatch ".(pl|php|cgi|spl|scgi|fcgi)$">
header unset cache-control
</filesmatch>

11. 压缩文件

压缩 text, html, javascript, css, xml

addoutputfilterbytype deflate text/plain
addoutputfilterbytype deflate text/html
addoutputfilterbytype deflate text/xml
addoutputfilterbytype deflate text/css
addoutputfilterbytype deflate application/xml
addoutputfilterbytype deflate application/xhtml+xml
addoutputfilterbytype deflate application/rss+xml
addoutputfilterbytype deflate application/javascript
addoutputfilterbytype deflate application/x-javascript

12. 防盗链

rewriteengine on
rewritecond %{http_referer} !^$
rewritecond %{http_referer} !^http://(www\.)?jb51.net(/)?.*$ [nc]
rewriterule .*\.(gif|jpg|jpeg|bmp|png)$ //www.jb51.net/src/logo.png [r,nc,l]

利用.htaccess实现网站图片防盗链功能

rewriteengine on
rewritecond %{request_uri} ^/images
rewriterule ^.*$ - [l]
rewritecond %{request_filename} \.(jpg|gif|bmp|zip|rar|mp3|txt|png|wma|wmv)$ [nc]
rewritecond %{http_referer} !^$
rewritecond %{http_referer} !^$ [nc]
rewritecond %{http_referer} !www.jb51.net [nc]
rewritecond %{http_referer} !jb51.net [nc]
rewritecond %{http_referer} !www.zhuaxia.com [nc]
rewritecond %{http_referer} !www.google.com [nc]
rewritecond %{http_referer} !google.com [nc]
rewritecond %{http_referer} !www.google.com.hk [nc]
rewritecond %{http_referer} !google.com.hk [nc]
rewritecond %{http_referer} !www.google.cn [nc]
rewritecond %{http_referer} !google.cn [nc]
rewritecond %{http_referer} !www.baidu.com [nc]
rewritecond %{http_referer} !www.baidu.cn [nc]
rewritecond %{http_referer} !baidu.com [nc]
rewritecond %{http_referer} !baidu.cn [nc]
rewritecond %{http_referer} !bloglines.com [nc]
rewritecond %{http_referer} !www.ask.com [nc]
rewritecond %{http_referer} !www.163.com [nc]
rewritecond %{http_referer} !www.yahoo.com [nc]
rewritecond %{http_referer} !www.sogou.com [nc]
rewritecond %{http_referer} !www.soso.com [nc]
rewritecond %{http_referer} !www.sina.com.cn [nc]
rewritecond %{http_referer} !www.msn.com [nc]
rewriterule (.*) /images/error.gif [r,nc,l]

代码说明:代码第二行,意思是将网站images目录的权限打开,也就是说images文件夹下的图片不进行防盗链,为什么呢?因为我们的盗链警告图片放在这个目录下,你要是不把这个目录权限打开,那么别人盗链的时候,连这张盗链警告图片也显示不出来,只能显示红叉叉,这不是我们想要的效果。代码第四行为需要防盗链文件的后缀名,可以自行根据需要修改。后边那些代码的意思是哪些网站可以不防盗链,都是一些搜索引擎之类的,还有自己站的网址。代码最后一行为盗链时显示的警告图片。也可以将这张图片存放到其他的网站,比如某些图片存储空间,这样的话就可以更加节省本站流量了。

13. 安全相关

rewriteengine on

# 阻止脚本企图通过url修改mosconfig值
rewritecond %{query_string} mosconfig_[a-za-z_]{1,21}(=|%3d) [or]
# 阻止脚本通过url传递的base64_encode垃圾信息
rewritecond %{query_string} base64_encode.*(.*) [or]
# 阻止在url含有<script>标记的脚本
rewritecond %{query_string} (<|%3c).*script.*(>|%3e) [nc,or]
# 阻止企图通过url设置php的globals变量的脚本
rewritecond %{query_string} globals(=|[|%[0-9a-z]{0,2}) [or]
# 阻止企图通过url设置php的_request变量的脚本
rewritecond %{query_string} _request(=|[|%[0-9a-z]{0,2})
# 把所有被阻止的请求转向到403禁止提示页面
rewriterule ^(.*)$ index.php [f,l]
# 禁止某些目录里的脚本执行权限
addhandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi
options -execcgi

.htaccess的特别说明

* 启用.htaccess,需要修改httpd.conf,启用allowoverride,并可以用allowoverride限制特定命令的使用
* 如果需要使用.htaccess以外的其他文件名,可以用accessfilename指令来改变。例如,需要使用.config ,则可以在服务器配置文件中按以下方法配置:

accessfilename .config 

* 一般情况下,不应该使用. htaccess文件,除非你对主配置文件没有访问权限。有一种很常见的误解,认为用户认证只能通过.htaccess文件实现,其实并不是这样,把用户认证写在主配置文件中是完全可行的,而且是一种很好的方法。.htaccess文件应该被用在内容提供者需要针对特定目录改变服务器的配置而又没有 root权限的情况下。如果服务器管理员不愿意频繁修改配置,则可以允许用户通过.htaccess文件自己修改配置,尤其是isp在同一个机器上运行了多个用户站点,而又希望用户可以自己改变配置的情况下。虽然如此,一般都应该尽可能地避免使用.htaccess文件。任何希望放在.htaccess文件中的配置,都可以放在主配置文件的<directory>段中,而且更高效。避免使用.htaccess文件有两个主要原因,即性能和安全。

附:
在线 .htaccess文件生成器|htaccess编辑器在线编辑器
能够在线生成. htaccess文件,很简单的就配置重定向,系统错误文件,自定义默认编码,错误页面等