网页技术交流
 
发新帖
楼主: 天启
查看: 1064|回复: 0

[使用教程] .htaccess文件常用使用手册

[复制链接]
天启VIP6 发表于 2019-12-10 14:44:10 | 显示全部楼层
设置错误页面
  1. ErrorDocument 400 /error_pages/400.html
  2. ErrorDocument 401 /error_pages/401.html
  3. ErrorDocument 403 /error_pages/403.html
  4. ErrorDocument 404 /error_pages/404.html
  5. ErrorDocument 500 /error_pages/500.html
复制代码

设置重定向
  1. #从 old_dir 目录重定向到 new_dir 目录
  2. Redirect /old_dir/ http://www.kaikuoyun.com/new_dir/index.html
  3. #把通过二级目录访问的请求 301 重定向到二级域名
  4. RedirectMatch 301 /dir/(.*) http://dir.kaikuoyun.com/$1
复制代码

禁止指定 IP 访问
  1. #禁止 IP 为 255.0.0.0 和 123.45.6.区段的 IP 访问
  2. order allow,deny
  3. deny from 255.0.0.0
  4. deny from 123.45.6.
  5. allow from all
复制代码

禁止指定来源访问

  1. #禁止从 kaikuoyun.com 和 www.kaikuoyun.com 的来源访问
  2. RewriteEngine on
  3. # Options +FollowSymlinks
  4. RewriteCond %{HTTP_REFERER} kaikuoyun\.com [NC,OR]
  5. RewriteCond %{HTTP_REFERER} www.kaikuoyun\.com
  6. RewriteRule .* - [F]
复制代码

文件防盗链

  1. #从本站以外的域名访问图片,一律显示 feed.jpg
  2. RewriteEngine on
  3. RewriteCond %{HTTP_REFERER} !^$
  4. RewriteCond %{HTTP_REFERER} !^http://(www\.)?kaikuoyun.com/.*$ [NC]
  5. RewriteRule \.(gif|jpg|png)$ http://www.kaikuoyun.com/feed.jpg [R,L]
复制代码

禁用文件夹列表

  1. #如果你的文件夹没有首页文件,服务器会显示文件列表,你可以设置不显示
  2. IndexIgnore *
  3. #仅不显示 .zip/.jpg/.gif 格式的文件
  4. IndexIgnore *.zip *.jpg *.gif
复制代码

设置文件夹首页

  1. #防止显示文件夹列表,当访问文件夹时,服务器查找 index.html 为首页文件,如不存在依次向后查找
  2. DirectoryIndex index.html index.cgi index.php
复制代码

设置媒体文件为可下载的而非播放

  1. AddType application/octet-stream .mp3 .mp4
复制代码

自定义 HTTP 报头

  1. Header set X-Pingback "http://www.kaikuoyun.com/xmlrpc.php"
  2. Header set article-by "kaikuoyun.com"
复制代码

设置文件过期时间 Cache Control

  1. # 启用有效期控制
  2. ExpiresActive On
  3. # gif/png/jpg 有效期为 1 个月
  4. ExpiresByType image/gif "access plus 1 month"
  5. ExpiresByType image/png "access plus 1 month"
  6. ExpiresByType image/jpg "access plus 1 month"
  7. # js/css 有效期为 1 星期
  8. ExpiresByType text/javascript "access plus 1 week"
  9. ExpiresByType text/css "access plus 1 week"
复制代码

判断User-agent 阻止迅雷下载

  1. ExpiresActive On
  2. RewriteCond %{HTTP_USER_AGENT} 2.0.50727 [NC]
  3. RewriteRule . 123.txt [L]
复制代码


快速回复 返回顶部 返回列表