仅限URL中的第一个文件夹的RewriteRule

时间:2016-11-03 12:19:00

标签: .htaccess redirect mod-rewrite

我必须将某些文件夹从我的域重定向到子域。这些是我的.htaccess规则:

RedirectMatch 301 /folder1  http://sub.domain.com/folder1$1
RedirectMatch 301 /folder2  http://sub.domain.com/folder2$1

一切正常,这会重定向

中的所有内容
www.domain.com/folder1/... => to => sub.domain.com/folder1/...

但没有挑战的世界是什么?我发现了这个问题:这条规则也重定向

www.domain.com/sub1/sub2/folder1/... => to => sub.domain.com/folder1/... 

在我看来这并不好。有什么办法可以避免这种情况吗?

提前完成。

1 个答案:

答案 0 :(得分:0)

您需要使用 ^ $ 来分隔您的正则表达式模式,以便仅匹配 / folder1

RedirectMatch 301 ^/folder1$ http://sub.domain.com/folder1$1