【问题标题】:Wildcard subdomains and CakePHP通配符子域和 CakePHP
【发布时间】:2013-09-20 01:58:07
【问题描述】:

我不擅长 .htaccess,因此我需要您的帮助。 最近我购买了通配符 SSL 证书,将 http 更改为 https 无法正常工作。 该站点基于 CakePHP 2.3 构建,public_html 目录具有以下结构:

/app
/lib
/plugins
/Cake
/sub1.domain.com
 - app
 - lib
 - plugins
 - ...
/sub2.domain.com
 - app
 - lib
 - plugins
 - ...

这是我的主机处理子域的方式。 文件夹 Cake 是跨所有子域 + 主域的共享 CakePHP 核心。

安装 SSL 后,cPanel 中自动添加了我无法更改的记录 *.domain.com -> /public_html

当我使用 http 时一切正常,但 https 将我的所有子域重定向到主域。 /public_html 中的 .htaccess 看起来像:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /

    # http|https www to non-www
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteRule ^(.*)$ http%1://domain.com/$1 [R=301,L]

    # redirect all subdomains
    RewriteCond %{HTTP_HOST} !^domain\.(.*)$ [NC]
    RewriteCond %{HTTPS} =on
    RewriteRule ^(.*)$ /%{HTTP_HOST}/$1 [NC,L,NS]

    RewriteCond %{HTTP_HOST} ^domain\.(.*)$ [NC]
    RewriteRule ^$ app/webroot/    [L]
    RewriteCond %{HTTP_HOST} ^domain\.(.*)$ [NC]
    RewriteRule (.*) app/webroot/$1 [L]
</IfModule>

子域中的所有 .htaccess 文件都是默认的带有 RewriteBase / 的 CakePHP 文件

有没有人遇到过类似的情况,你是怎么解决的? 请提供帮助,因为它真的超出了我的知识范围。

更新 #4

/public_html/sub1.domain.com/.htaccess 是:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /

    RewriteCond %{HTTPS} =on
    RewriteRule ^$ sub1.domain.com/app/webroot/    [L]
    RewriteCond %{HTTPS} =on
    RewriteRule (.*) sub1.domain.com/app/webroot/$1 [L]

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

一切看起来都不错,只是 https://sub1.domain.com 重定向到 https://sub1.domain.com/sub1.domain.com 并且所有生成的路径都包含/sub1.domain.com

如何从 URL 和链接中删除文件夹 sub1.domain.com?

我应该更新Configure::write('App.baseUrl', env('SCRIPT_NAME')); 的其他内容吗? 仍在尝试修复它

【问题讨论】:

    标签: .htaccess subdomain cakephp-2.3


    【解决方案1】:

    终于在 1 周后(在其他任务之间)我解决了这个问题!

    这是我的解决方案:

    /public_html/.htaccess

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteBase /
    
        # http|https www to non-www
        RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
        RewriteCond %{HTTPS}s ^on(s)|
        RewriteRule ^(.*)$ http%1://domain.com/$1 [R=301,L]
    
        # redirect all subdomains
        RewriteCond %{HTTP_HOST} !^domain\.(.*)$ [NC]
        RewriteCond %{HTTPS} on
        RewriteRule ^(.*)$ /%{HTTP_HOST}/$1 [NC,L,NS]
    
        RewriteCond %{HTTP_HOST} ^domain\.(.*)$ [NC]
        RewriteRule ^$ app/webroot/    [L]
        RewriteCond %{HTTP_HOST} ^domain\.(.*)$ [NC]
        RewriteRule (.*) app/webroot/$1 [L]
    </IfModule>
    

    /public_html/sub1.domain.com/.htaccess

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteBase /
    
        RewriteCond %{REQUEST_URI} ^/%{HTTP_HOST}
        RewriteRule ^%{HTTP_HOST}(.*)$ /$1
    
        RewriteCond %{HTTPS} on
        RewriteRule ^$ %{HTTP_HOST}/app/webroot/    [L]
        RewriteCond %{HTTPS} on
        RewriteRule (.*) %{HTTP_HOST}/app/webroot/$1 [L]
    
        RewriteRule ^$ app/webroot/    [L]
        RewriteRule (.*) app/webroot/$1 [L]
    </IfModule>
    

    /public_html/sub1.domain.com/app/.htaccess

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

    /public_html/sub1.domain.com/app/webroot/.htaccess

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

    最后是最重要的!

    /public_html/sub1.domain.com/app/webroot/index.php

    底部:$Dispatcher = new Dispatcher();改为:$Dispatcher = new Dispatcher('');

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-12
      • 2020-08-23
      • 2013-08-18
      • 2011-06-05
      • 2016-10-12
      • 2010-09-09
      • 2019-03-18
      • 2011-10-03
      相关资源
      最近更新 更多