上一篇文章提到了如何使用Redirect做重定向,但是Redirect做的话不能匹配一批URL,这就需要用到RedirectMatch和正则表达式,来完成更艰巨的任务。

RedirectMatch指令:

说明: 基于正则表达式匹配对当前的URL发送一个外部重定向 
语法: RedirectMatch    [status]     regex     URL 
上下文: 服务器配置, 虚拟主机, 目录, .htaccess 
覆盖项: FileInfo 
状态: Base 
模块: mod_alias 
说明:此指令与
Redirect等效, 但是它使用了标准的正则表达式,而不是简单的前缀匹配。 如果此正则表达式与URL-path相匹配, 则服务器会用给定的字符串替换加了括弧的匹配,并视之为一个文件名

 举例来说,比如我要把以下地址全部匹配到http://www.frankdu.com/blog/feed上:

/blog/SyndicationService.asmx/GetRssCategory
/blog/SyndicationService.asmx/GetRssCategory?categoryName=Coding
/blog/SyndicationService.asmx/GetRssCategory?categoryName=Natural Language Processing
/blog/SyndicationService.asmx/GetRssCategory?categoryName=Coding|Python
/blog/SyndicationService.asmx/GetRss

那么可以在.htaccess中添加下面一行指令:

RedirectMatch 301 /blog/SyndicationService.asmx.*$ http://www.frankdu.com/blog/feed

现在访问原来地址的链接,都能找到新的、改变后的地址了。

更多内容,请参见Apache的mod_alias模块的中文文档