2012-04-21 133 views
1

我有一個子域(store.example.com)。我不僅要重定向來自store.example.com的流量,還要將其重定向到域上的特定文件(我能找到的所有內容均基於重定向整個子域)使用htaccess從子域重定向到域(單個文件)

所以我需要store.example。 com/Science-Fiction - > www.bundoranpress.com/category/1/Science-Fiction

我會如何使用htaccess文件編寫此文件?

回答

0

您將需要一些RewriteConds或(在我看來更好的方法)處理您的請求的PHP腳本。對於第一種方法:

RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC] 
RewriteCond %{REQUEST_URI} ^(ThanksForAllTheFish) [NC] 
RewriteRule (.*) http://www.bundoranpress.com/$1/$2 [L,R] 

這將重定向到store.example.com/ThanksForAllTheFish銷售www.bundoranpress.com/store/ThanksForAllTheFish道格拉斯亞當的書。 現在,你可以去將所有可能的方面在第二的RewriteCond像這樣:

RewriteCond %{REQUEST_URI} ^(ThanksForAllTheFish|Universe|Hitchhiking) [NC] 

這將捕獲的條件之一,並重定向到相應的地址(例如www.bundoranpress.com/store/Universe) 。你看這很快就會很難。 一個更好的辦法是從一個特定的文件夾重定向所有 URL和子域名PHP腳本處理其餘部分,像這樣:

RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC] 
RewriteCond %{REQUEST_URI} ^books/(.*) [NC] 
RewriteRule (.*) http://www.bundoranpress.com/index.php?sub=$1&cat=$2 [L,R] 

這store.example.com/books/ThanksForAllTheFish重定向到www .bundoranpress.com/index.php?sub = store & cat = ThanksForAllTheFish。你可以分別通過$ _GET [「sub」]和$ _GET [「cat」]來訪問變量。這爲您提供了更大的靈活性。