2015-10-17 138 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

相关问题