.htaccess 文件使用手册

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

1、apache开启.htaccess

vi编辑httpd.conf文件,修改这两个地方:

(1)

Options FollowSymLinks

AllowOverride None

改为

Options FollowSymLinks

AllowOverride All

(2)去掉下面的注释

LoadModule rewrite_module modules/mod_rewrite.so

2、.htaccess的写法:

注意:在项目根目录下创建,.htaccess文件 权限设置为644

利用.htaccess指定事先制作好的错误提醒页面。一般下,人们专门设立目录,例如errors放置页面。然后再.htaccess中,加入如下的指令:

ErrorDocument 404 /errors/notfound.html

ErrorDocument 500 /errors/internalerror.html

一条指令一行。上述第一条指令的意思是对于404,也找到所的文档的得显示页面为/errors目录下的notfound.html页面。不难看出语法格局为:

ErrorDocument 错误代码 /目录名/名.扩展名

所提示的很少的话,不必专门制作页面,直接在指令中HTML号了,例如下面例子:

ErrorDocument 401 “你权限访问该页面,请抛却!”

重写 rewrite:

#rewrite ^/??$ http://ue.alipay.com last;

#rewrite ^/index.(htm|html|php)??$ http://ue.alipay.com last;

#rewrite ^/diaochae_([0-9]+).html??$ /admin/index.php?r=survey/view&wid=$1&page=end last;

#rewrite ^/feed.html??$ /admin/?r=feedback/add last;

#rewrite ^/error.html??$ /admin/index.php last;

#rewrite ^/adminlogin.html??$ https://bumng.alipay.com last;

#rewrite ^/adminloginlocal.html??$ http://bumng.test.alipay.net last;

#rewrite ^/admin.html??$ /admin/index.php?r=survey/list last;

#rewrite ^/login.html??$ /admin/index.php?r=survey/login last;

#rewrite ^/diaochach_([0-9]+).html??$ /admin/index.php?r=survey/view&wid=$1 last;

#rewrite ^/diaocha_([0-9]+).html??$ /admin/index.php?r=survey/view&wid=$1 last;

#rewrite ^/diaochae_([0-9]+).html&sign_from=3000$ /admin/index.php?r=survey/view&wid=$1&page=end&sign_from=3000 last;

#rewrite ^/diaocha_([0-9]+).html&sign_from=3000??$ /admin/index.php?r=survey/view&wid=$1&sign_from=3000 last;

#rewrite ^/diaochach_([0-9]+).html&sign_from=3000$ /admin/index.php?r=survey/view&wid=$1&sign_from=3000 last;

什么是 .htaccess 文件?

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

——站长百科

理解WordPress 的 htaccess

# BEGIN WordPress #这是一行注释,表示 WordPress 的 htaccess 从这里开始
 #如果 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 
# END WordPress #WordPress 的 htaccess 到这里结束

使用范例

设置错误页面

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

设置重定向

#从 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访问

#禁止 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

禁止指定来源访问

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

文件防盗链

#从本站以外的域名访问图片,一律显示 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]

禁用文件夹列表

#如果你的文件夹没有首页文件,服务器会显示文件列表,你可以设置不显示
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.yourdomain.com/xmlrpc.php"
Header set article-by "c7sky.com"

设置文件过期时间 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"

免责声明:文章转载自《.htaccess 文件使用手册》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇JavaScript-Runoob:JavaScript 数据类型PHP (20140522)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Oracle如何迁移、管理、清除Audit数据(AUD$和FGA_LOG$表)

Oracle如何迁移、管理、清除Audit数据(AUD$和FGA_LOG$表) 前言 版本:11.2.0.4.0。 Oracle 11G中没特殊要求还是要建议关闭审计功能:alter system set audit_trail = none scope=spfile  sid='*'; 由于默认审计数据的AUD$和FGA_LOG$表在system表空间,...

nginx实战二

 nginx架构分析 1.nginx模块化 Nginx涉及到的模块分为核心模块、标准HTTP模块、可选HTTP模块、邮件服务模块以及第三方模块等五大类。 https://coding.net/u/aminglinux/p/nginx/git/blob/master/4z/module.md [root@centos-03 objs]# ls ngx_mo...

mysql主从之keepalive+MySQL高可用

一 keepalive介绍 1.1 keepalived 是什么 keepalived 是集群管理中保证集群高可用的一个服务软件,用来防止单点故障。 1.2 keepalived 工作原理 keepalived 是以VRRP 协议为实现基础的,VRRP 全称VirtualRouter Redundancy Protocol,即虚拟路由冗余协议。虚拟路由冗...

MySQL主从报错1872 Slave failed to initialize relay log info structure from the repository【原创】

一台MySQL服务器重启后发现主从复制报错 Last_Errno: 1872 Last_Error: Slave failed to initialize relay log info structure from the repository   mysql> show slave statusG; ************************...

loadrunner---<二>---菜鸟对cookie的思考

http://www.cnblogs.com/Pierre-de-Ronsard/archive/2012/11/19/2772630.html loadrunner---<二>---菜鸟对cookie的思考 lr是怎么将cookie添加到录制的脚本中的?lr中cookie是做什么的? 首先将解决两个疑问: 1--什么是cookie?-- C...

C++ ORM ODB入门

1.ORM ORM, Object Relational Mapping, 对象关系映射,用来将基于对象的数据结构映射到SQL的数据结构中。即将基于对象的数据映射到关系表中的字段,然后我们可以通过对象提供的接口来操作数据库,而无需写sql语句来操作数据库。一般一张关系表对应两个类,一个实体类和一个操作类。ORM是一种框架,而不是一种实现。 2.C++ 的O...