通配符子域和CakePHP

时间:2013-09-16 01:17:38

标签: .htaccess subdomain cakephp-2.3

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

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

我应该更新Configure::write('App.baseUrl', env('SCRIPT_NAME'));其他内容吗? 还在努力解决它

1 个答案:

答案 0 :(得分:4)

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

这是我的解决方案:

/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('');