Apache - mod_rewrite规则详解

一,简介:

Apache的rewrite模块,提供了一个基于正则表达式规则的重写引擎,用来(on the fly)实时修改传入的请求的 URL 。因功能极其强大,被称为URL重写的“瑞士军刀”。

它支持无限的规则,以及为每个规则附加条件,从而提供了一个真正灵活且强大的 URL 操作机制。URL 操作可以依赖于各种测试,例如服务器变量,环境变量,HTTP 头,时间戳,甚至外部数据库查询等,以便完成 URL 单元匹配。

这个模块可以操作完整的 URL (包含目录信息部分和查询字符串部分) ,在服务器上下文 (httpd.conf)、虚拟主机上下文 (<VirtualHost> 指令块)、目录上下文 (.htaccess 文件和 <Directory> 指令块) 都可以配置。重写的结果 URL,可以指向一个站内的处理程序、指向站外的重定向或者一个站内的代理。

既然 mod_rewrite 这么强大,它当然是相当复杂。因此,别指望一天之内就能看懂整个模块。

二,打开Apache的rewrite功能

1、LoadModule
1)在windows环境下:
打开您的apache安装目录“/apache/conf/” 下的 httpd.conf 文件,通过Ctrl+F查找到LoadModule rewrite_module modules/mod_rewrite.so,将前面的”#”号删除即可
2)在linux环境下:
在编译 apache 的时候记得加上带 rewrite 模块。

2、让apache服务器支持.htaccess
在服务器或者虚拟主机的<Directory>配置段里,把你的AllowOverride配置设置成All,表示允许所有指令在 .htaccess 生效。

3、检查rewrite模块是否开启
当rewrite模块已经成功加载时,在phpinfo()里可以看到load的模块列表里有rewrite的名字。

三,特殊字符

1)$N,引用RewriteRule模板中匹配的相关字串,N表示序号,N=0..9
2)%N,引用最后一个RewriteCond模板中匹配的数据,N表示序号
3)%{VARNAME},服务器变量
4)${mapname:key|default},映射函数调用

四,指令

Apache Rewrite 的重写规则的具体指令共有 RewriteBase, RewriteCond, RewriteEngine, RewriteLock, RewriteLog, RewriteLogLevel, RewriteMap, RewriteOptions, RewriteRule 九个指令。

下面我们就最常用的RewriteEngine, RewriteBase, RewriteCond, RewriteRule这四个指令重点讲解。

1、RewriteEngine指令

原文 译文
描述(Description) Enables or disables runtime rewriting engine 开启或关闭重写引擎
语法(Syntax) RewriteEngine on|off
默认(Default) RewriteEngine off
作用域/上下文(Context) server config, virtual host, directory, .htaccess

2、RewriterRule规则
一条RewriteRule指令,定义一条重写规则,规则间的顺序非常重要。对Apache1.2及以后的版本,模板(pattern)是一个 POSIX正则式,用以匹配当前的URL。当前的URL不一定是用记最初提交的URL,因为可能用一些规则在此规则前已经对URL进行了处理。

原文 译文
描述(Description) Defines rules for the rewriting engine
语法(Syntax) RewriteRule Pattern Substitution [Flag1,Flag2,Flag3]
作用域/上下文(Context) server config, virtual host, directory, .htaccess

指令说明:匹配部分(Pattern) 是正则匹配URL的正则表达式(注意特殊字符需要转义处理), 可以在替换部分(Substitution)使用反向引用匹配部分的内容. 引用模式为: $N (N为1-9的整数)。

先说明一下一个比较特别的 Substitution 值: "-", 如果Substitution是 "-" 的话, 那么被请求的URL不会被修改掉,只做匹配检查。

在URL重写的匹配部分中,服务器会把请求的URL的一部分删除掉再传递给Pattern部分进行匹配,重写结束后再添加上去。所以平常我们看到的匹配规则总是不带网址前面的那些域名的什么东西的,也不带什么目录什么的,这些 apache已经给删掉了,处理完后再加到前面。但是有个例外,就是如果 Substitution 部分是带 http:// 开头的话, 那就直接重定向了,服务器不会把先前删除的再给加上了,不然就出错了。

查看几个官方的例子:
例如请求地址为: http://thishost/somepath/pathinfo, 看下面几种结果

Given Rule (重写规则) Resulting Substitution (替换结果)
^/somepath(.*) otherpath$1 invalid, not supported(无效的重写语句,不支持)
^/somepath(.*) otherpath$1 [R] invalid, not supported
^/somepath(.*) otherpath$1 [P] invalid, not supported
^/somepath(.*) /otherpath$1 /otherpath/pathinfo
^/somepath(.*) /otherpath$1 [R] http://thishost/otherpath/pathinfo via external redirection(外部重定向)
^/somepath(.*) /otherpath$1 [P] doesn't make sense, not supported
^/somepath(.*) http://thishost/otherpath$1 http://www.test.com/otherpath/pathinfo
^/somepath(.*) http://thishost/otherpath$1 [R] http://thishost/otherpath/pathinfo via external redirection(外部重定向)
^/somepath(.*) http://thishost/otherpath$1 [P] doesn't make sense, not supported
^/somepath(.*) http://otherhost/otherpath$1 http://otherhost/otherpath/pathinfo via external redirection(外部重定向)
^/somepath(.*) http://otherhost/otherpath$1 [R] http://otherhost/otherpath/pathinfo via external redirection (the [R] flag is redundant)(外部重定向,[R] 标志是多余的)
^/somepath(.*) http://otherhost/otherpath$1 [P] http://otherhost/otherpath/pathinfo via internal proxy(内部网关重定向)
还有 72% 的精彩内容
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 重写规则 【重写规则的作用范围】 可以使用在Apache主配置文件httpd.conf中 可以使用在httpd.c...
    桖辶殇阅读 5,711评论 0 0
  • 看了一些关于.htaccess,apache重写url的规则,一般都写得很清楚。但是对于我一个小白,感觉要挖的东西...
    yancolin阅读 10,930评论 0 3
  • 此文章转载~ Apache伪静态(Rewrite).htaccess文件详解 Htaccess(超文本访问)是一个...
    bornfreekiss阅读 9,631评论 0 6
  • apache下的htaccess伪静态文件 bornfreekiss 关注 2017.10.23 11:17* 字...
    其实我很dou阅读 7,739评论 0 3
  • 前几天,偶和一位曾在体制内工作的年轻马头琴手聊天(他现早已转行)。在谈到马头琴时,他问我:为什么现在的他琴技早已退...
    陆春宁阅读 4,082评论 8 5