2011-09-01 37 views
11

我有一個域和一個子域(用於移動設備和客戶端)的共享主機。每個域和子域具有不同的默認索引頁面。由於我無法訪問httpd.conf,託管公司告訴我將所有內容都放入我的.htaccess文件中。根據.htaccess中的域/子域更改DirectoryIndex

我想這樣做是:

  1. 如果用戶去domain1.com的的DirectoryIndex應該是:index.html
  2. 如果用戶去mobile.domain1.com的DirectoryIndex應該是: mobile-index.html
  3. 如果用戶去post.domain1.com的DirectoryIndex應該是:post.php
  4. 如果用戶去vote.domain1.com的DirectoryIndex應該是:vote.php

編輯: 另外,如果我去domain1.com/page/的DirectoryIndex應該是:index.html。如果我去mobile.domain1.com/page/的DirectoryIndex應該是:mobile-index.html

我可以把我的.htaccess文件以什麼改變DirectoryIndex每個子域?

非常感謝你密歇根州

回答

18

<IfDefine>不喜歡的工作。 <IfDefine>只在apache啓動時運行。你應該使用mod_rewrite解決方案。查看@tzakrajs的答案。

你可以在你的.htaccess文件中使用此:

SetEnvIf Host ^www\. page=www 
SetEnvIf Host ^mobile\. page=mobile 
rewriterule ^.*$ test.php?subdomain=%{ENV:page} [QSA,L] 

只是簡單地使用SetEnvIf配置所有的子域,然後讓PHP做它的魔力。

+2

感謝您的澄清。感謝SetEnvIf,我認爲這樣更容易實現(而不是創建4個不同的文件)。謝謝 – Tech4Wilco

+0

換句話說:在Apache啓動後'''不是動態的。 – Geremia

2

試試這個:

RewriteEngine on 
RewriteBase/
RewriteCond %{HTTP_HOST} ^domain1.com$ 
RewriteRule ^.*/$ index.html [R=302,L] 
RewriteCond %{HTTP_HOST} ^mobile.domain1.com$ 
RewriteRule ^.*/$ mobile-index.html [R=302,L] 
RewriteCond %{HTTP_HOST} ^post.domain1.com$ 
RewriteRule ^.*/$ post.php [R=302,L] 
RewriteCond %{HTTP_HOST} ^vote.domain1.com$ 
RewriteRule ^.*/$ vote.php [R=302,L] 
+0

感謝,對着陸頁的作品,但它並不適用於子文件工作。例如mobile.domain1.com/page/應該加載mobile-index.html – Tech4Wilco

+0

好吧,我修改了規則,以便它們能匹配任何子目錄(例如:domain1.com/fdskjfkls/將重定向到index.html) – tzakrajs

+0

它確實但不重定向到:domain1.com/fdskjfkls/index.html和mobile.domain1.com/fdskjfkls/mobile.html – Tech4Wilco

-1

您可以設置只用oyur .htaccess文件是這樣的:

RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC] 
RewriteRule DirectoryIndex index.html 

RewriteCond %{HTTP_HOST} ^mobile.domain.com$ [NC] 
RewriteRule DirectoryIndex mobile-index.html 

... 
+0

這不工作.... – Laxman

+0

@laxman你不需要投票只是因爲它不適合你 – CIRCLE