Using multiple subdomain for each app

I have a host with an SSL subdomain (wildcard) certificate where I want to store multiple projects (both Yii apps as static pages). All the subdomains point to the same document root, so I need to make sure that each subdomain is pointed to a subdirectory where a website runs.

I’m having trouble getting this to work using .htaccess, I want to use paths for my controller (host.com/news/index instead of host.com?r=news/index).

This is the current folder structure:

Here is a simplified version of the .htaccess I’m running




RewriteEngine on

###################################################


#yii site

RewriteCond %{HTTP:Host} ^(?:www\.)?mysite1\.mainwebsite\.com$

RewriteCond %{REQUEST_URI} !^/mysite1/

RewriteRule ^css/(.*) /mysite2/public_html/css/$1 [L,NC]

RewriteRule ^assets/(.*) /mysite2/public_html/assets/$1 [L,NC]

RewriteRule ^(.*) mysite1/public_html/index.php/$1 [L]


#non yii site

RewriteCond %{HTTP:Host} ^(?:www\.)?mysite2\.mainwebsite\.com$

RewriteCond %{REQUEST_URI} !^/mysite2/

RewriteRule ^(.*) mysite2/$1 [NC,L,NS]


#yii site

RewriteCond %{HTTP:Host} ^(?:www\.)?mysite3\.mainwebsite\.com$

RewriteCond %{REQUEST_URI} !^/mysite3/

RewriteRule ^css/(.*) /mysite3/public_html/css/$1 [L,NC]

RewriteRule ^assets/(.*) /mysite3/public_html/assets/$1 [L,NC]

RewriteRule ^(.*) mysite3/public_html/index.php/$1 [L]



After hours of fiddling with the different settings, I came across a lot of problems:

  • some rewriterule catches ALL the incoming request, making it work only for one app

  • the complete document root is appended in the URL, like


http://mysite1.mainwebsite.com/webroot/mysite1/public_html/news/

  • static files are rewritten to the index.php

etc

Does anyone already have a similar htaccess running that can forward each subdomain to a Yii-app in a reliable way?

Another option that crossed my mind is to use Yii for rewriting subdomains, it could send the right requests to each subdomain using URL rules. But I have no idea if this is possible in an elegant way.