linux .htaccess Redirect Match 匹配重定向用法大全

| | Comments (0)



linux .htaccess Redirect Match 匹配重定向用法 大全

前提:

当我们需要从一个域名改到另一个域名,或者对服务器的目录结构进行调整时,你的用户还是从老的目录进来,就可能会遇到找不到文件,或是访问禁止等等问题。
如何解决这个问题?

这时候RedirectMatch就显得非常有用,如果你租用的虚拟主机是apache,那么恭喜你,你也可以使用目录下的.htaccess里设置RedirectMatch。

语法: RedirectMatch [status] regex URL
regex 为 regular expressions 的缩写,具体参考 Apache 手册。

应用: 服务器配置, 虚拟主机, 目录, .htaccess文件

举例:

1) 将一个目录重定向到一个文件:
RedirectMatch 301 ^/lastdir$ /lastdir.html

2) 将A目录重定向到B目录:
RedirectMatch 301 ^/dir_a$ /dir_b

3) 将A目录下所有的文件重定向到B目录:
RedirectMatch 301 ^/dir_a/.* /dir_b


4) 将A目录下所有的文件重定向到B目录相对应的文件:
RedirectMatch 301 ^/dir_a/(.*) /dir_b/$1
$1表示上面圆括弧中的变量,如果有多个圆括弧,则按顺序为 $2,$3

5) 将A目录下所有的文件重定向到B服务器的C目录相对应的文件:
RedirectMatch 301 ^/dir_a/(.*) http://spoint.babyshoot.cn/dir_c/$1
这个对有些原先使用个人空间,而现在有了自己的服务器或者虚拟主机的人来说非常有用,
比如原先是 spoint.babyshoot.cn/~mypage
而现在有了www.babyshoot.cn 这个空间
那么就可以在原个人空间的目录下编辑 .htaccess 加入:
RedirectMatch 301 ^/~dir_a/(.*) www.babyshoot.cn/$1
如果域名发生变更,可以这样:
RedirectMatch 301 ^(.*) http://www.babyshoot.cn/$1

说明:
符号 ^ 表示匹配项的开始, 符号 $ 表示结束,符号 * 代表通配符,符号 () 定义变量,$1, $2 为变量名。
301是状态码,表示永久重定向,另外还有:
302,临时重定向,如果不写状态码,则这个就是默认值。
303,系统会有一个页面,指出资源地址已经改变。
410,表示资源地址已经永久删除

单点日志:http://spoint.babyshoot.cn

Leave a comment

Archives

Ads by google

Pages

Powered by Movable Type 4.24-en

About this Entry

This page contains a single entry by 单点日志 published on June 4, 2011 1:19 AM.

苏芮-搭错车 酒干倘卖无 was the previous entry in this blog.

法国戴维斯杯 网球公开赛 女单冠军 中国李娜 2-0完胜 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.