2011-07-13 27 views
0

我最近移動了我的博客,我只是301重定向幾頁。麻煩是我發現了一個惱人的問題。301 .htaccess奇怪的行爲和完全匹配

有兩個環節 -

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它的主頁。

問題是,這個重定向影響了第一次重定向,可能是因爲它與第一次重定向非常相似。

任何想法如何我只能301重定向精確匹配?

謝謝!

+0

什麼是該命令你已經給出了規則。 – Balanivash

+0

只要刪除'hxxp:// www.domain.com'部分,你就很好:'Redirect'指令的OLD_URL部分應該以斜槓開頭,不應該包含域名:http://httpd.apache.org/docs /current/mod/mod_alias.html#redirect – LazyOne

回答

1

AFAIK重定向總是重定向目錄到

您可以使用ModRewrite

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

重定向1頁

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

所有目錄重定向到1(主)頁面

0

對於這種匹配你想使用mod_rewrite,swiss a URL的RMY刀改寫;-)

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

如果放在'.htaccess'中,這將不起作用 - URL模式不包含前導斜槓'/'。但如果放置在配置或虛擬主機上下文中,它將正常工作。 – LazyOne

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標誌(代表最後)強制Apache將停止處理其他改寫指令

+0

如果放置在'.htaccess'中,這些RewriteRules將不起作用 - URL模式將不包含前導斜槓'/'。但如果放置在配置或虛擬主機上下文中,它將正常工作。 – LazyOne

+0

我不知道這種差異。然後你可以用'^'替換前導的'/' – user842313