.htaccess 文件使用手册

 时间:2026-02-15 17:50:07

1、设置错误页面

ErrorDocument400 /error_pages/400.html

ErrorDocument401 /error_pages/401.html

ErrorDocument403 /error_pages/403.html

ErrorDocument404 /error_pages/404.html

ErrorDocument500 /error_pages/500.html

2、设置重定向

#从 old_dir 目录重定向到 new_dir 目录Redirect /old_dir/ http://www.yourdomain.com/new_dir/index.html

#把通过二级目录访问的请求301重定向到二级域名RedirectMatch301 /dir/(.*) http://dir.yourdomain.com/$1

3、禁止指定IP访问

#禁止 IP 为 255.0.0.0 和 123.45.6.区段的 IP 访问orderallow,denydeny from 255.0.0.0

deny from 123.45.6.

allow from all

4、禁止指定来源访问

#禁止从 otherdomain.com 和 anotherdomain.com 的来源访问RewriteEngineon# Options +FollowSymlinksRewriteCond %{HTTP_REFERER} otherdomain\.com [NC,OR]

RewriteCond %{HTTP_REFERER} anotherdomain\.com

RewriteRule .* - [F]

5、文件防盗链

#从本站以外的域名访问图片,一律显示 feed.jpgRewriteEngineonRewriteCond %{HTTP_REFERER} !^$

RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com/.*$ [NC]

RewriteRule \.(gif|jpg|png)$ http://www.yourdomain.com/feed.jpg [R,L]

6、禁用文件夹列表

#如果你的文件夹没有首页文件,服务器会显示文件列表,你可以设置不显示IndexIgnore *

#仅不显示 .zip/.jpg/.gif 格式的文件IndexIgnore *.zip *.jpg *.gif

7、设置文件夹首页

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

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

AddType application/octet-stream .mp3 .mp4

9、自定义 HTTP 报头

Header set X-Pingback "http://www.xxx.cn/xmlrpc.php"Header set article-by "xxx.cn"

10、设置文件过期时间 Cache Control

<IfModule mod_expires.c>

# 启用有效期控制ExpiresActiveOn# gif/png/jpg 有效期为1个月ExpiresByType image/gif "access plus 1 month"ExpiresByType image/png "access plus 1 month"ExpiresByType image/jpg "access plus 1 month"# js/css 有效期为1星期ExpiresByType text/javascript "access plus 1 week"ExpiresByType text/css "access plus 1 week"

</IfModule>

11、基本配置方法都在上面了 各位有不懂的欢迎提问~

  • win7中如何建.htaccess文件
  • Apache下.htaccess文件的常用配置
  • 直接创建.htaccess的方法
  • htaccess在线制作
  • .htaccess必须键入文件名,怎么解决
  • 热门搜索
    怎么隐藏任务栏 荷兰牛栏奶粉怎么样 厕所堵了怎么通 外痔怎么治疗 被蛇咬了怎么办 耳鸣怎么治疗 侃侃怎么读 信封封面怎么写 压力太大怎么办 怎么把图片上的字去掉