2011-04-17 53 views
1

我試圖使用htaccess重定向任何子域和一個域的根到另一個域。 例如htaccess從任何子域重定向到另一個域和從目錄到查詢網址

anysub.mysite.com或mysite.com重定向到www.anotheriste.com

並且還mysite.com/stuffhere到www.anothersite.com/feed?v=stuffhere

所以任何不是子目錄的東西都會被重定向到其他域,任何作爲子目錄的東西都會被重定向到帶有查詢字符串的URL。似乎無法得到它。一個重寫規則覆蓋另一個。

編輯: 這是我得到了什麼工作

Options +FollowSymlinks 
RewriteEngine on 
RewriteRule ^$ http://www.site.com [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} ^/*\/? 
RewriteRule ^(.*)$ http://www.site.com/feed?v=$1 [R=301,L] 

回答

2

嘗試把這些規則在您的.htacccess文件:

RewriteEngine on 
Options +FollowSymlinks -MultiViews 

# handles http redirect sub-dir 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteCond %{SERVER_PORT} =80 
RewriteCond %{HTTP_HOST} ^(.+\.)?mysite\.com$ [NC] 
RewriteRule ^/?(.*)$ http://www.anotheriste.com/feed?v=$1 [R=301,L,QSA,NE] 

# handles http redirect non sub-dir 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{SERVER_PORT} =80 
RewriteCond %{HTTP_HOST} ^(.+\.)?mysite\.com$ [NC] 
RewriteRule ^/?(.*)$ http://www.anotheriste.com/$1 [R=301,L,QSA,NE] 

# handles https redirect sub-dir 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteCond %{SERVER_PORT} =443 
RewriteCond %{HTTP_HOST} ^(.+\.)?mysite\.com$ [NC] 
RewriteRule ^/?(.*)$ https://www.anotheriste.com/feed?v=$1 [R=301,L,QSA,NE] 

# handles https redirect non sub-dir 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{SERVER_PORT} =443 
RewriteCond %{HTTP_HOST} ^(.+\.)?mysite\.com$ [NC] 
RewriteRule ^/?(.*)$ https://www.anotheriste.com/$1 [R=301,L,QSA,NE] 

R = 301將與HTTPS狀態重定向301

L將做出最後規則

NE不會逃避查詢Ÿ串

QSA將追加現有的查詢參數

$ 1的REQUEST_URI

+0

他感謝的答案,但它確實沒有如預期那麼回事。原因是,我忽略了提及目錄不是真實的。所以mysite.com/stuffhere不是一個實際的目錄。我所做的一部分是短網址,所以這些目錄實際上並不存在。經過大量的搜索後,我想出瞭如何完成我所需要的。我編輯了我的帖子,以顯示我的工作。如果他們真的是目錄,我相信你們會工作得很好。感謝那。 – 2011-04-20 06:46:24

+0

是的,這是正確的我的答案是基於實際目錄的存在。真的很高興你將它變成了一個可行的解決方案。 – anubhava 2011-04-21 03:10:50

相關問題