2011-12-06 18 views
0

我現在有一個子域我需要保持機智:www.sub.domain.com我如何重定向只使用htaccess的根?

我也有在www.domain.com/blog需要保持完整

這是一個子目錄一個令人討厭的設置,因爲我使用的是第三方使用CNAMES託管的shopify。我的店鋪位於shop.domain.com,我的博客位於domain.com/blog

我只想重定向root/domain.com(www和非www),但是如果他們登陸任何東西在/(例如博客文章)之後。它將重定向到子域。

這是我目前

ErrorDocument 404 /index.html 
RewriteEngine on 
rewritecond %{http_host} ^domain.com [nc] 
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc] 

回答

4

這個怎麼樣?

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com [NC] 
RewriteRule ^$ <wherever you want to send the redirect> [NC,R=301] 
+0

請注意,在.htaccess中,前導斜槓被刪除,因此ReqriteRule模式'^/$'永遠不會匹配。用'^ $'替換來得到你想要的結果。 –

+0

烏爾裏希 - 很好,你完全正確。正確答覆更新。謝謝! – ziesemer

+0

謝謝!將盡快嘗試。 – Tallboy

0

我有同樣的問題,這htaccess的代碼爲我工作 RewriteEngine敘述在

RewriteCond %{HTTP_HOST} ^sub\.domain\.com [NC] #root 
RewriteRule ^$ http://www.newsite.com/news-trends/ [NC,R=301] 
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC] #else 
RewriteRule ^(.*) http://www.newsite.com/blog/$1 [L,R] 
0

這應該去!

# First we turn the rewriting engine on 
RewriteEngine on 

# Then we check if the host is the right one 
RewriteCond %{HTTP_HOST} (www\.)?domain\.com [NC] 

# Then we make sure only the home page is considered 
RewriteCond %{REQUEST_URI} ^/$ 

# Then we redirect wherever we want! 
Rewriterule ^(.*)$ http://othersite.com/ [L,R=301] 
+0

只是想知道...你怎麼找到像4歲的線程?大聲笑 – Tallboy