【问题标题】:Redirecting subdomain to a folder inside main that contains a CakePHP app将子域重定向到 main 中包含 CakePHP 应用程序的文件夹
【发布时间】:2016-01-15 20:50:12
【问题描述】:

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

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

有效的解决方案,在 /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>

【问题讨论】:

  • 您使用的是共享主机吗?
  • 设置另一个 VirtualHost 进行改造并将 DocumentRoot 设置为 /revamp
  • @DavidAguilar 我正在使用 VPS 是的
  • @user2182349 我一直被告知我的网络主机不要从 vhost 更改根目录,因为它会在每次 apache 需要重建时重置,他们建议我使用 htaccess 额外规则来引导寻找子域的流量
  • 将 revamp 放入第一个文件夹的 app/webroot 文件夹不是更容易吗?有理由不这样做吗?

标签: php .htaccess cakephp


【解决方案1】:

您可以通过使用“重写条件”为不同的不同域实现所需的重写:

在我的机器上,我创建了一个文件夹结构,例如

public_html
 -app
   -webroot
 -local
   -app
     -webroot

public_html 文件夹中的 app、webroot 和本地目录,然后是本地文件夹中的 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 文件夹中。

.htaccessapp/ 目录中的代码

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

.htaccesswebroot/ 中的代码

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

【讨论】:

  • 它尝试了一个简单的 index.html 文件,而不是子域中的 cakephp 站点
  • @ChrisF 它适用于 cakephp 的文件夹结构,因为我创建了上面提到的结构,对于简单的文件夹,您需要更改 htaccess 文件中的代码
  • @ChrisF 请查看修改后的答案
  • @ChrisF 使用最后一个 .htaccess 代码作为子域,希望它对你有用
  • @ChrisF 你可以试试RewriteCond %{HTTP_HOST} ^test\.com$ [OR] RewriteCond %{HTTP_HOST} ^www\.test\.com$
【解决方案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>

参考资料:

【讨论】:

  • 我从这个解决方案中得到 ERR_TOO_MANY_REDIRECTS
猜你喜欢
  • 2019-03-07
  • 2012-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-27
  • 2017-12-05
  • 1970-01-01
相关资源
最近更新 更多