栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > 服务器

.htaccess文件使用教程总结

服务器 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

.htaccess文件使用教程总结

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 文件

order allow,deny
deny from all

# 阻止查看所有文件

order allow,deny
deny from all

# 阻止查看指定的文件

order allow,deny
deny from all

# 多种文件类型

order allow,deny
deny from all

5. 重定向

Redirect permanent / https://www.jb51.net
Redirect temp /old.html https://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. 使用/禁用缓存文件


Header set Cache-Control "max-age=2592000"


Header unset Cache-Control

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)$ https://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含有