打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
.htaccess 文件使用手册

什么是 .htaccess 文件?

概述来说,htaccess 文件是 Apache 服务器中的一个配置文件,它负责相关目录下的网页配置。 通过 htaccess 文件,可以帮我们实现:网页301重定向、自定义404错误页面、改变文件扩展名、允许/阻止特定的用户或者目录的访问、禁止目录列表、配置默认文档等功能。

使用范例

设置错误页面

ErrorDocument 400 /error_pages/400.htmlErrorDocument 401 /error_pages/401.htmlErrorDocument 403 /error_pages/403.htmlErrorDocument 404 /error_pages/404.htmlErrorDocument 500 /error_pages/500.html

设置重定向

#从 old_dir 目录重定向到 new_dir 目录Redirect /old_dir/ http://www.482223.com/new_dir/index.html#把通过二级目录访问的请求301重定向到二级域名RedirectMatch 301 /dir/(.*) http://dir.482223.com
/$1

禁止指定IP访问

#禁止 IP 为 255.0.0.0 和 123.45.6.区段的 IP 访问order allow,denydeny from 255.0.0.0deny from 123.45.6.allow from all

禁止指定来源访问

#禁止从 otherdomain.com 和 anotherdomain.com 的来源访问RewriteEngine on# Options +FollowSymlinksRewriteCond %{HTTP_REFERER} otherdomain\.com [NC,OR]RewriteCond %{HTTP_REFERER} anotherdomain\.comRewriteRule .* - [F]

文件防盗链

#从本站以外的域名访问图片,一律显示 feed.jpgRewriteEngine onRewriteCond %{HTTP_REFERER} !^$RewriteCond %{HTTP_REFERER} !^http://(www\.)?482223.com/.*$ [NC]RewriteRule \.(gif|jpg|png)$ http://www.482223.com/feed.jpg [R,L]

禁用文件夹列表

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

设置文件夹首页

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

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

AddType application/octet-stream .mp3 .mp4

自定义 HTTP 报头

Header set X-Pingback "http://www.482223.com/xmlrpc.php"Header set article-by "kuaifenxiang.net"

设置文件过期时间 Cache Control

# 启用有效期控制ExpiresActive On# 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"
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
善用.htaccess文件成为网站利器
htaccess 实现301重定向
您的WordPress网站的15个有用的.htaccess片段
.htaccess重定向和url重写详细介绍
15个有用的WordPress .htaccess 代码片段 | WordPress大学
几个常用的301重定向 - 网页
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服