将子域重定向到main中包含CakePHP应用程序的文件夹


Redirecting subdomain to a folder inside main that contains a CakePHP app

在SO中尝试了很多例子,但似乎都不起作用。考虑我的设置:

/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:

被要求创建一个vhost,我做到了,它被设置为子域文件夹,我想要的帮助是一个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:

将cakephp站点置于/public_html/app/public_html/revamp/app

<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>

您可以通过对不同的域使用"Rewrite condition"来实现所需的重写:

在我的机器上,我创建了一个文件夹结构,如
public_html
 -app
   -webroot
 -local
   -app
     -webroot

app, webroot和本地目录在public_html文件夹,然后app和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的结构,我将.htaccess文件分别放在两个应用程序的appwebroot文件夹中。

app/目录下.htaccess的代码

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

webroot/.htaccess的代码

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,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.html,而subdomain.test.com将打开subdomain文件夹的index.html

更新答案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>

Try

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>

引用:

  • 使web根文件夹与。htaccess的子文件夹?
  • https://webmasters.stackexchange.com/questions/35726/how-to-redirect-requests-to-another-folder-using-htaccess