htaccess 文件使用手册&教程
2013-12-22 02:34
253 查看
什么是 .htaccess 文件? 概述来说,htaccess 文件是 Apache 服务器中的一个配置文件,它负责相关目录下的网页配置。 通过 htaccess 文件,可以帮我们实现:网页301重定向、自定义404错误页面、改变文件扩展名、允许/阻止特定的用户或者目录的访问、禁止目录列表、配置默认文档等功能。
理解 WordPress 的 htaccess
PHP
# BEGIN WordPress #这是一行注释,表示 WordPress 的 htaccess 从这里开始
<IfModule mod_rewrite.c> #如果 Apache 加载了 mod_rewrite.c 模块,则运行以下代码
RewriteEngine On #启用 mod_rewrite 引擎
RewriteBase / #设置目录重写的基准URL为 /
RewriteRule ^index\.php$ - [L] #如果请求路径是 index.php,停止重写操作(避免死循环)
RewriteCond %{REQUEST_FILENAME} !-f #如果请求的不是一个文件,继续处理
RewriteCond %{REQUEST_FILENAME} !-d #如果请求的不是一个目录,继续处理
RewriteRule . /index.php [L] #把所有的请求指向 /index.php
</IfModule> #结束 IfModule
# END WordPress #WordPress 的 htaccess 到这里结束
使用范例 设置错误页面
Shell
ErrorDocument 400 /error_pages/400.html
ErrorDocument 401 /error_pages/401.html
ErrorDocument 403 /error_pages/403.html
ErrorDocument 404 /error_pages/404.html
ErrorDocument 500 /error_pages/500.html
设置重定向
Shell
#从 old_dir 目录重定向到 new_dir 目录
Redirect /old_dir/ http://www.yourdomain.com/new_dir/index.html #把通过二级目录访问的请求301重定向到二级域名
RedirectMatch 301 /dir/(.*) http://dir.yourdomain.com/$1
禁止指定IP访问
PHP
#禁止 IP 为 255.0.0.0 和 123.45.6.区段的 IP 访问
order allow,deny
deny from 255.0.0.0
deny from 123.45.6.
allow from all
禁止指定来源访问
PHP
#禁止从 otherdomain.com 和 anotherdomain.com 的来源访问
RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} otherdomain\.com [NC,OR]
RewriteCond %{HTTP_REFERER} anotherdomain\.com
RewriteRule .* - [F]
文件防盗链
PHP
#从本站以外的域名访问图片,一律显示 feed.jpg
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com/.*$ [NC]
RewriteRule \.(gif|jpg|png)$ http://www.yourdomain.com/feed.jpg [R,L]
禁用文件夹列表
PHP
#如果你的文件夹没有首页文件,服务器会显示文件列表,你可以设置不显示
IndexIgnore *
#仅不显示 .zip/.jpg/.gif 格式的文件
IndexIgnore *.zip *.jpg *.gif
设置文件夹首页
PHP
#防止显示文件夹列表,当访问文件夹时,服务器查找 index.html 为首页文件,如不存在依次向后查找
DirectoryIndex index.html index.cgi index.php
设置媒体文件为可下载的而非播放
PHP
AddType application/octet-stream .mp3 .mp4
自定义 HTTP 报头
PHP
Header set X-Pingback "http://www.yourdomain.com/xmlrpc.php"
Header set article-by "yu1u.org"
参考资料
The WordPress .htaccess File Explained
Apache HTTP 服务器 2.4 文档
THE Ultimate Htaccess
.htaccess Guide
Comprehensive guide to .htaccess
Apache HTTP Server Tutorial: .htaccess files
理解 WordPress 的 htaccess
PHP
# BEGIN WordPress #这是一行注释,表示 WordPress 的 htaccess 从这里开始
<IfModule mod_rewrite.c> #如果 Apache 加载了 mod_rewrite.c 模块,则运行以下代码
RewriteEngine On #启用 mod_rewrite 引擎
RewriteBase / #设置目录重写的基准URL为 /
RewriteRule ^index\.php$ - [L] #如果请求路径是 index.php,停止重写操作(避免死循环)
RewriteCond %{REQUEST_FILENAME} !-f #如果请求的不是一个文件,继续处理
RewriteCond %{REQUEST_FILENAME} !-d #如果请求的不是一个目录,继续处理
RewriteRule . /index.php [L] #把所有的请求指向 /index.php
</IfModule> #结束 IfModule
# END WordPress #WordPress 的 htaccess 到这里结束
1 2 3 4 5 6 7 8 9 10 | # BEGIN WordPress #这是一行注释,表示 WordPress 的 htaccess 从这里开始 <IfModule mod_rewrite.c>#如果 Apache 加载了 mod_rewrite.c 模块,则运行以下代码 RewriteEngine On#启用 mod_rewrite 引擎 RewriteBase/#设置目录重写的基准URL为 / RewriteRule^index\.php$-[L]#如果请求路径是 index.php,停止重写操作(避免死循环) RewriteCond%{REQUEST_FILENAME}!-f#如果请求的不是一个文件,继续处理 RewriteCond%{REQUEST_FILENAME}!-d#如果请求的不是一个目录,继续处理 RewriteRule./index.php[L]#把所有的请求指向 /index.php </IfModule>#结束 IfModule # END WordPress #WordPress 的 htaccess 到这里结束 |
Shell
ErrorDocument 400 /error_pages/400.html
ErrorDocument 401 /error_pages/401.html
ErrorDocument 403 /error_pages/403.html
ErrorDocument 404 /error_pages/404.html
ErrorDocument 500 /error_pages/500.html
1 2 3 4 5 | 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 |
Shell
#从 old_dir 目录重定向到 new_dir 目录
Redirect /old_dir/ http://www.yourdomain.com/new_dir/index.html #把通过二级目录访问的请求301重定向到二级域名
RedirectMatch 301 /dir/(.*) http://dir.yourdomain.com/$1
1 2 3 4 | #从 old_dir 目录重定向到 new_dir 目录 Redirect/old_dir/http://www.yourdomain.com/new_dir/index.html #把通过二级目录访问的请求301重定向到二级域名 RedirectMatch301/dir/(.*)http://dir.yourdomain.com/$1 |
PHP
#禁止 IP 为 255.0.0.0 和 123.45.6.区段的 IP 访问
order allow,deny
deny from 255.0.0.0
deny from 123.45.6.
allow from all
1 2 3 4 5 | #禁止 IP 为 255.0.0.0 和 123.45.6.区段的 IP 访问 order allow,deny deny from255.0.0.0 deny from123.45.6. allow from all |
PHP
#禁止从 otherdomain.com 和 anotherdomain.com 的来源访问
RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} otherdomain\.com [NC,OR]
RewriteCond %{HTTP_REFERER} anotherdomain\.com
RewriteRule .* - [F]
1 2 3 4 5 6 | #禁止从 otherdomain.com 和 anotherdomain.com 的来源访问 RewriteEngineon # Options +FollowSymlinks RewriteCond%{HTTP_REFERER}otherdomain\.com[NC,OR] RewriteCond%{HTTP_REFERER}anotherdomain\.com RewriteRule.*-[F] |
PHP
#从本站以外的域名访问图片,一律显示 feed.jpg
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com/.*$ [NC]
RewriteRule \.(gif|jpg|png)$ http://www.yourdomain.com/feed.jpg [R,L]
1 2 3 4 5 | #从本站以外的域名访问图片,一律显示 feed.jpg RewriteEngineon RewriteCond%{HTTP_REFERER}!^$ RewriteCond%{HTTP_REFERER}!^http://(www\.)?yourdomain.com/.*$ [NC] RewriteRule\.(gif|jpg|png)$http://www.yourdomain.com/feed.jpg [R,L] |
PHP
#如果你的文件夹没有首页文件,服务器会显示文件列表,你可以设置不显示
IndexIgnore *
#仅不显示 .zip/.jpg/.gif 格式的文件
IndexIgnore *.zip *.jpg *.gif
1 2 3 4 | #如果你的文件夹没有首页文件,服务器会显示文件列表,你可以设置不显示 IndexIgnore* #仅不显示 .zip/.jpg/.gif 格式的文件 IndexIgnore*.zip*.jpg*.gif |
PHP
#防止显示文件夹列表,当访问文件夹时,服务器查找 index.html 为首页文件,如不存在依次向后查找
DirectoryIndex index.html index.cgi index.php
1 2 | #防止显示文件夹列表,当访问文件夹时,服务器查找 index.html 为首页文件,如不存在依次向后查找 DirectoryIndex index.html index.cgi index.php |
PHP
AddType application/octet-stream .mp3 .mp4
1 | AddType application/octet-stream.mp3.mp4 |
PHP
Header set X-Pingback "http://www.yourdomain.com/xmlrpc.php"
Header set article-by "yu1u.org"
1 2 | Header setX-Pingback"http://www.yourdomain.com/xmlrpc.php" Header set article-by"yu1u.org" |
The WordPress .htaccess File Explained
Apache HTTP 服务器 2.4 文档
THE Ultimate Htaccess
.htaccess Guide
Comprehensive guide to .htaccess
Apache HTTP Server Tutorial: .htaccess files
相关文章推荐
- IOS学习笔记 运用Quartz 2D框架绘图理论基础(1)
- Matlab计算机视觉/图像处理工具箱推荐
- cocos2d 各种字体名称及其效果
- 十进制转十六进制 - C
- Himi浅谈游戏开发de自学历程!(仅供参考)
- npm install —— 从一个简单例子,看本地安装与全局安装的区别
- 嗅探器的实现 对协议的分析(转)
- vimball
- 智慧城市方法_主頁
- 智慧城市<顶层设计>方法_主頁
- DNS扫盲系列之一:有关公网DNS
- WCF REST 根据请求距离,返回附近用户的实例
- DNS扫盲系列之二:域名解析及DNS功能分类
- DNS扫盲系列之三:有关智能DNS(BIND+VIEW)
- Makefile系列之三 : 变量
- DNS扫盲系列之四:关注域名迁移
- DNS扫盲系列之五:域名配置ZONE文件
- POJ2406 Power Strings KMP算法
- DNS扫盲系列之六:擅用日志排除BIND故障
- 《编写可维护的JavaScript》读书笔记第5章:UI层的松耦合