301 .htaccess奇怪的行为和完全匹配

时间:2011-07-13 09:45:14

标签: .htaccess redirect

我最近移动了我的博客,而我只是重定向几页。麻烦的是我发现了一个恼人的问题。

有两个链接 -

hxxp://www.domain.com/category/post.html
hxxp://www.domain.com/category/

我想301到一个新的域名,所以写了以下内容......

Redirect 301 hxxp://www.domain.com/category/post.html  hxxp://new.domain.com/category/post.html

工作正常,然后我做了类别......

Redirect 301 hxxp://www.domain.com/category/ hxxp://new.domain.com/

由于类别结构不同,我只想将其填入主页。

麻烦的是,这个重定向正在影响第一次重定向,大概是因为它与第一次重定向非常相似。

任何想法我怎么只能301重定向完全匹配?

谢谢!

3 个答案:

答案 0 :(得分:1)

AFAIK Redirect始终将目录重定向到目录

您可以使用ModRewrite

RewriteRule ^category/$ http://newdomain/ [R=301] 

重定向1页

RewriteRule ^category/.*$ http://newdomain/ [R=301]

将所有目录重定向到1(主)页面

答案 1 :(得分:0)

对于这种匹配,你想使用mod_rewrite,URL重写的瑞士军刀; - )

RewriteEngine On
RewriteRule /category/post.html$ http://new.domain/post.html [L,R=301]
RewriteRule /category.* http://new.domain/ [L,R=301]

答案 2 :(得分:0)

您可以尝试使用RedirectMatch指令(see docs here),该指令允许您使用正则表达式来匹配要重定向的URL。这样,您可以在末尾添加$符号以指定字符串的结尾:

RedirectMatch 301 hxxp://www.domain.com/category/post.html$ hxxp://new.domain.com/category/post.html
RedirectMatch 301 hxxp://www.domain.com/category/$ hxxp://new.domain.com/

这样,重定向只会影响完全匹配。

或者,您可以使用RewriteRule指令(docs here),如下所示:

RewriteEngine On
RewriteRule /category/post.html$  hxxp://new.domain.com/category/post.html [ R=301, L ]
RewriteRule /category/$  hxxp://new.domain.com/ [ R=301, L ]

L标志(代表Last)强制Apache停止处理其他Rewrite指令