2015-10-17 79 views
11

在SO中嘗試了很多示例,但似乎沒有任何效果。我考慮設立:將子域重定向到包含CakePHP應用程序的main文件夾中

/public_html/ 
    /app/ 
    /cgi-bin/ 
    /lib/ 
    /plugins/ 
    /revamp/ 
    /vendors/ 

我現在有一個CakePHP的網站site.com/app/文件夾下它的文件,我現在使用權的.htaccess看起來是這樣的:

<IfModule mod_rewrite.c> 
    RewriteEngine on 

    ReWriteRule ^$ app/webroot/ [L] 
    ReWriteRule (.*) app/webroot/$1 [L] 
</IfModule> 

我想重定向子域revamp.site.com到文件夾/revamp/這將是另一個cakephp網站。

更新1:

被要求創建一個虛擬主機和我一樣,它被設置爲子域文件夾,幫助我要的是一個htaccess的規則,兩個工作,一個在上面,也重定向到子域如果請求的地址有域之前,它的子域...

更新2:

以下.htaccess讓我到我的子域,但只能中的index.html

<IfModule mod_rewrite.c> 
    RewriteEngine on 

    RewriteCond %{HTTP_HOST} ^site\.com$ 
    RewriteRule (.*) app/webroot/$1 [L] 

    RewriteCond %{HTTP_HOST} ^revamp\.site\.com$ 
    RewriteRule (.*) revamp/index.html [L] 
</IfModule> 

它是兩個答案的混合...儘管無論我將哪個路徑放在子域中,它只會讓我到index.html(因爲它在htaccess中是硬編碼的)。也試過revamp/$revamp/$1沒有運氣。

更新3:

試圖與第一個答案的編輯:

<IfModule mod_rewrite.c> 
    RewriteEngine on 

    #subdomain simple site 
    RewriteCond %{HTTP_HOST} ^subdomain\.test\.com$ 
    RewriteCond %{REQUEST_URI} !^/subdomain 
    RewriteRule ^(.*)$ subdomain/$1 [L] 

    #cakephp site 
    RewriteRule ^$ app/webroot/ [L] 
    RewriteRule (.*) app/webroot/$1 [L] 
</IfModule> 

仍給500,我也試過兩個塊的開關位置

更新4:

WORKING解決方案,在/public_html/app/public_html/revamp/app之間都有cakephp網站

<IfModule mod_rewrite.c> 
    RewriteEngine on 

    RewriteCond %{HTTP_HOST} ^revamp\.site\.com$ 
    RewriteRule ^(.*)$ revamp/app/webroot/$1 [NC,L] 

    RewriteRule ^$ app/webroot/ [L] 
    RewriteRule (.*) app/webroot/$1 [NC,L] 
</IfModule> 
+0

您使用的共享主機? –

+0

設置另一個VirtualHost進行修改並將DocumentRoot設置爲/修復 – user2182349

+0

@DavidAguilar我正在使用VPS yes – nullwriter

回答

3

您可以通過使用「重寫條件」不同的不同的域achive需要改寫:

在我的機器我cretaed的文件夾結構類似

public_html 
-app 
    -webroot 
-local 
    -app 
    -webroot 

應用程序,Web根目錄和本地目錄中public_html文件夾,然後在本地文件夾中的應用程序和webroot目錄。

現在,我創建瞭如下一個虛擬主機條目:

<VirtualHost test.com:80> 
ServerName test.com 
ServerAlias local.test.com 
DocumentRoot /var/www/html/public_html 

ErrorLog ${APACHE_LOG_DIR}/error.log 
CustomLog ${APACHE_LOG_DIR}/access.log combined 
</VirtualHost> 

手段test.comlocal.test.com都指着public_html目錄。

現在你的目標是,如果域名是test.com那麼外部應用程序應該運行,如果域名是local.test.com那麼你的內部(本地文件夾)應用程序應該運行。

由於cakephp中.htaccess的結構,我在appwebroot兩個應用程序的文件夾中都放置了.htaccess文件。在app/目錄的.htaccess

代碼在webroot/

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule ^(.*)$ index.php [QSA,L] 
</IfModule> 

.htaccess

<IfModule mod_rewrite.c> 
    RewriteEngine on 
    RewriteRule ^$ webroot/ [L] 
    RewriteRule (.*) webroot/$1 [L] 
</IfModule> 

代碼現在在根目錄即.htaccess的變化public_html

<IfModule mod_rewrite.c> 
    RewriteEngine on 
    #for test.com 
    RewriteCond %{HTTP_HOST} ^test\.com$ 
    RewriteRule ^(.*)$ app/webroot/$1 [NC,L] 

    #for local.test.com 
    RewriteCond %{HTTP_HOST} ^local\.test\.com$ 
    RewriteRule ^(.*)$ local/app/webroot/$1 [NC,L] 
</IfModule> 
  1. 的RewriteCond %{HTTP_HOST} ^test\.com$將確定該域是test.com
  2. 的RewriteCond %{HTTP_HOST} ^local\.test\.com$將確定 域名local.test.com

對於一般的子域重寫你可以使用如下代碼:

文件夾結構:

public_html 
-index.html 
-subdomain 
    --index.html 

.htaccess

<IfModule mod_rewrite.c> 
    RewriteEngine on 

    #for subdomain.test.com 
    RewriteCond %{HTTP_HOST} ^subdomain\.test\.com$ 
    RewriteCond %{REQUEST_URI} !^/subdomain 
    RewriteRule ^(.*)$ subdomain/$1 [L] 
</IfModule> 

代碼現在test.com將打開public_html文件夾的index.htmlsubdomain.test.com將打開index.htmlsubdomain文件夾的

更新應答2

我創建了問題中提到確切的目錄結構,並且下面的htaccess代碼工作對我來說:

<IfModule mod_rewrite.c> 
    RewriteEngine on 

    RewriteCond %{HTTP_HOST} ^test\.com$ 
    RewriteRule ^(.*)$ app/webroot/$1 [NC,L] 

    RewriteCond %{HTTP_HOST} ^revamp\.test\.com$ 
    RewriteCond %{REQUEST_URI} !^/revamp 
    RewriteRule ^(.*)$ revamp/$1 [NC,L] 
</IfModule> 
+0

這給了我一個500錯誤的子域 – nullwriter

+0

而不是在子域內cakephp網站,它嘗試了一個簡單的index.html文件 – nullwriter

+0

@ChrisF它將爲CakePHP中的文件夾結構工程,我創建的結構像上面提到的,對於簡單的文件夾,你需要更改代碼htaccess文件 –

2

嘗試

RewriteCond %{HTTP_HOST} ^revamp\.site\.com$ 
RewriteRule (.*) revamp.site.com/$1 [R=301,L] 
RewriteRule ^$ revamp [L] 

不過,我想創建一個普通的新VirtualHost處理請求的子域

<VirtualHost *:80> 
    ServerName revamp.site.com 
    DocumentRoot /path/to/public_html/revamp 
    <Directory> 
     #your other rewrite rules here 
    </Directory> 
</VirtualHost> 

參考文獻:

+0

我越來越從這個ERR_TOO_MANY_REDIRECTS解決方案 – nullwriter

相關問題