本篇文章主要介绍 .htaccess 文件的编写。
博主博客
自定义错误页面
- 400 错误请求
- 401 认证失败
- 402 需要付费
- 403 禁止访问(Forbidden)
- 404 未找到页面(Not Found)
- 405 方法不允许
- 406 不可接受
- 407 需要代理认证
- 408 请求超时
- 409 请求冲突
htaccess 文件格式
<Files ~ "^.(htaccess|htpasswd)$">
deny from all
</Files>
# 是否显示文件夹目录
# Options +Indexes # 显示
Options -Indexes # 隐藏
# 是否开启图片缓存
<FilesMatch ".(gif|jpg|jpeg|png|ico)$">
Header set Cache-Control "max-age=600" # max-age 为时间, 单位秒(s)
</FilesMatch>
# 自定义错误页面, 此处添加404.html跟404.html
ErrorDocument 400 /400.html
ErrorDocument 404 /404.html
# 自定义默认文档, 这里加入index.html跟index.php
DirectoryIndex index.html index.php
# www 域名重定向, 此处使用了百度, 如果有需要, 可以改成自己的
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(baidu\.com)(:80)? [NC]
RewriteRule ^(.*) http://www.baidu.com/$1 [R=301,L]
# 设置图片防盗链
# RewriteEngine on # 如果上面使用了, 这里就不需要使用
RewriteCond %{HTTP_REFERER} !^$
# 允许以下域名访问图片
RewriteCond %{HTTP_REFERER} !^http://(www\.)?baidu.com(/)?.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?hao123.com(/)?.*$ [NC]
# 非法盗链图片替换的路径, 该路径不得使用上面域名, 不然会造成循环
RewriteRule .*\.(gif|jpg|jpeg|bmp|png)$ http://www.360.com/photo.jpg [R,NC,L]
# 网页重定向, /old.html 跳转到 http://www.old.com/new.html
# 301 Moved Permanently 永久重定向
Redirect permanent /old.html http://www.old.com/new.html
# 302 Moved Temporarily 临时重定向
Redirect temp /old.html http://www.old.com/new.html
# 访问权限, 允许 allow from + ip
allow from 192.168.0.50
# 拒绝 deny from + ip
deny from 192.168.0.50
order deny,allow
# 自定义MIME类型, 文件后缀: php3, 类型: application/x-httpd-php
AddType application/x-httpd-php php3
在线生成 .htaccess 网站推荐:
http://www.cnfree.org/htaccess/