1

我不擅长 .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 将我的所有子域重定向到主域。.htaccess/public_html看起来像:

<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 文件都是默认的 CakePHP 文件RewriteBase /

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

更新#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'));其他内容吗?仍在尝试修复它

4

1 回答 1

4

终于在 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('');

于 2013-09-27T07:42:28.440 回答