Configuring routes to handle primary and wildcard subdomains traffic for www site

Hi to All forum members.

This is my first post on Yii forum.

I have a website that runs on Yii 1.1 framework. The site is active for 2 years and now I want to support subdomains feature for all the registered users. My htaccess is configured to redirect all traffic to www site i.e if I type into URL then it gets redirected to www,domain,com and my primary website is rendered. I have changed my DNS settings and added * record and added this in routes in URL Manager:

        '<username:\w+>,domain,com' => 'public/profile'

which means that if user types URL with subdoman then he is sent to PublicController’s profile action. But now the problem is that all of my traffic even for primary domain is going to this route. i.e


As a result of that I am unable to access my primary website. Here is my htaccess code:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php

# add www if not subdomain

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$

RewriteRule ^ http///www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# remove www if subdomain

RewriteCond %{HTTP_HOST} ^www\.([^.]+\.[^.]+\.[^.]+)$ [NC]

RewriteRule ^ http///%1%{REQUEST_URI} [R=301,L]

How do I support both the things that when user types username,domain,com it routes to only public/profile route and all other traffic (www,domain,com) traffic runs as normal.

I have been searching for solution for 2 days. Would greatly appreciate any help.


Note: Since I am new to forum and not allowed to post links so I am replacing dot (.) with comma (,) wherever URL is being used.

Perhaps "standard" rules prepended with literal "www".

Is public/profile the same for both the sub domain and regular domain?

For example is the same as

the reason being your subdomain route matches all your requests even if its not a subdomain you still have www prefixed which is a match for \w+

# right here this adds www if there is no www typed by user

# add www if not subdomain

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$

RewriteRule ^ http///www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I would suggest you move your other routes above your subdomain route if you have any or tweak your subdomain regex so it discards www.

'<controller:\w+>/<action:\w+>' => '<controller>/<action>'

'<username:\w+>,domain,com' => 'public/profile'

hope that helps

Thank you very much for the response. I have made progress on this and almost all is fixed with just one problem.

I need to support two routes only via subdomain. These are:



Note: please note that the word ‘param’ is dynamic string and it can be replaced by any string like http/// All works totally fine with following Yii URL manager rules and htaccess configuration:

'http///<username:(?!www\.)\w+>.domain,com' => 'public/profile',

'http///<username:(?!www\.)\w+>.domain,com/<slug:([A-Za-z0-9-.]+)>' => 'public/slug/<slug:\w+>'

.htacces code:


RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php

if not main domain skip all rules

RewriteCond %{HTTP_HOST} !^(?:www\.)?domain\,com$ [NC]

RewriteRule ^ - [L]

RewriteCond %{HTTP_HOST} !^www\. [NC]

RewriteRule ^ %{REQUEST_SCHEME}://www,%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]


The only problem left is that all of my controller actions are accessible subdomain urls as well like:

http///james,domain,com/about should not be accessible via subdomain as it is only main website route (http///www,domain,com/about)

If the ‘param’ after is not valid route then it works fine. But if param is the name of some action then it runs that page from main site which I don’t want. For example:

Some of my main site routes are (should be accessible only when subdomain is www):





But they are accessible like this as well:





I highly feel that this is due to order of URL manager rules in Yii config.

Here are my routes array in config:


        '/infographic' =&gt; 'site/infographic',

        '/howitworks' =&gt; 'site/howitworks',

        '/how-to-download-after-purchase' =&gt; 'site/howtodownload',

        '/howitworks/custom' =&gt; 'site/customhow',

        '/faqs' =&gt; 'site/page/type/faqs',

        '/about' =&gt; 'site/page/type/about',

        '/terms/' =&gt; 'site/page/type/terms',

        '/privacypolicy' =&gt; 'site/page/type/privacypolicy',

        '/useragreement' =&gt; 'site/page/type/useragreement',

        '/accessibility' =&gt; 'site/page/type/accessibility',

        '/forgotpassword' =&gt; 'site/forgotpassword',

        '/register' =&gt; 'site/register',

        '/resendactivation' =&gt; 'site/resendactivation',

        '/resetpassword' =&gt; 'site/resetpassword',

        '/activate' =&gt; 'site/activate',

        'http///&lt;username:(?&#33;www&#092;.)&#092;w+&gt;,domain,com' =&gt; 'public/profile',

        'http///&lt;username:(?&#33;www&#092;.)&#092;w+&gt;,domain,com/&lt;slug:([A-Za-z0-9-.]+)&gt;' =&gt; 'public/slug/&lt;slug:&#092;w+&gt;',

        '&lt;controller:&#092;w+&gt;/&lt;id:&#092;d+&gt;' =&gt; '&lt;controller&gt;/view',

        '&lt;controller:&#092;w+&gt;/&lt;action:&#092;w+&gt;/&lt;id:&#092;d+&gt;' =&gt; '&lt;controller&gt;/&lt;action&gt;',

        '&lt;controller:&#092;w+&gt;/&lt;action:&#092;w+&gt;' =&gt; '&lt;controller&gt;/&lt;action&gt;',


Apologies again for replacing dots (.) and colon (:) as I am not allowed to post links due to forum rules.

Have you tried putting the sub domain yii rules first? Normally the specific rules that could fall under another rule should be first so they match the right rule. It looks like your rules are hitting the /about and stopping because your /about doesn’t care what the domain is and yours is technically all one site so placing the it first might do the trick.

Just fyi you can group a lot of your rules http://www.yiiframew…terizing-routes and your terms rule has a trailing slash which makes it a different url than without it.