Wildcard subdomains and CakePHP

拟墨画扇 提交于 2019-12-23 03:15:24

问题


I'm not strong in .htaccess and for this reason I need your help. Recently I purchased wildcard SSL certificate and changing http to https doesn't work properly. The site is build on CakePHP 2.3 and public_html directory has a following structure:

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

It is how my hosting handle subdomains. Folder Cake is a shared CakePHP core across all subdomains + main domain.

When SSL was installed, in cPanel was automatically added record *.domain.com -> /public_html that I can't change.

Everything works fine when I use http, but https redirect all my subdomains on main domain. .htaccess in /public_html looks like:

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

All .htaccess files in subdomains are default CakePHP files with RewriteBase /

Did someone had similar situation and how did you solve it? Please help as it really outside of my knowledge.

Update #4

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

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

Everything looks ok, only https://sub1.domain.com redirects to https://sub1.domain.com/sub1.domain.com and all generated paths are includes /sub1.domain.com

How can I remove folder sub1.domain.com from URL and from links?

Should I update Configure::write('App.baseUrl', env('SCRIPT_NAME')); on something else? Still trying to fix it


回答1:


Finally after 1 week (between other tasks) I solved this!

Here is my solution:

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

And finally the most important!

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

On the bottom: $Dispatcher = new Dispatcher(); changed to: $Dispatcher = new Dispatcher('');



来源:https://stackoverflow.com/questions/18819192/wildcard-subdomains-and-cakephp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!