.htaccess 文件格式

本篇文章主要介绍 .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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nukix

如果觉得对你有帮助,欢迎打赏!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值