What is the best directory structure to adopt when setting up a website based on this url structure:

www.example.com - home page subdomain1.example.com subdomain2.example.com subdomain3.example.com

I am told this is difficult to setup with expressionengine out of the box and needs some fancy .htaccess code to rewrite urls e.t.c. If i didn't want to go that route, what are my options?

is this easier if each subdomain had its own ee install? Do i need to use some sort of CNAME mapping? ideally, I'd like all sub pages under each subdomain to correctly show the url structure i.e "subdomain1.example.com/page1" vs "www.example.com/subdomain/page1"

Thx

  • 1
    Good question. I have a client with this need as well, and the only option I found with a real easy setup was MSM. Curious to see what others have to say about this. – Justin Kimbrell Aug 6 '13 at 14:15

Looks like you need Multiple Site Manager.

But, if all your subdomains are part of the same website, there's a technique I use on multiple language websites that can help you.

You have two possible setups.

On the first one, your directories need to follow this structure:

  • /system
  • /domain.com
  • /domain.com/images
  • /domain.com/themes
  • /domain.com/index.php
  • /domain.com/admin.php
  • /sub.domain.com
  • /subdomain.com/index.php
  • /sub2.domain.com
  • /sub2.domain.com/index.php

Each index.php has its own settings about the url of the website.

The second and my favorite option is to set all domains to use the same folder on server and add a conditional on system/expressionengine/config.php to set all the variables you need. For example, you can have a specific default category_group or channel for every domain.

global $assign_to_config;

if(!isset($assign_to_config['global_vars']))
$assign_to_config['global_vars'] = array();

// set the site_url to the current domain being accessed
$assign_to_config['site_url']   = 'http://'.$_SERVER['HTTP_HOST'];

// different settings and variables for every domain
if ($_SERVER['HTTP_HOST'] == 'www.domain.com' || $_SERVER['HTTP_HOST'] == 'domain.com')
{
    $assign_to_config['xml_lang']   = 'en';
    $assign_to_config['language']   = 'en';
    $assign_to_config['global_vars']['cv_cat_group'] = 1;
    $assign_to_config['global_vars']['cv_channel'] = 'main_domain';
    [...]
}
else
{
    $assign_to_config['xml_lang'] = 'pt';
    $assign_to_config['language'] = 'pt';
    $assign_to_config['global_vars']['cv_cat_group'] = 2;
    $assign_to_config['global_vars']['cv_channel'] = 'subdomain';
    [...]
}

You can access this custom config variables right on templates: {cv_cat_group} and {cv_channel}. They are early parsed, so you can use them inside your modules variables pairs:

{exp:channel:entries channel="{cv_channel}" [...]}
  [...]
{/exp:channel:entries}

As I said, this technique works really well for me.

If you can't use the same directory to all sites, use the setup I suggested on option 1.

  • Hi Robson - thx for your comments. I like option 2 but a bit over my head. Can you elaborate a bit more? Are there any specific folders / directories setup for each subdomain? Is there anything to be done ( like .htaccess ) besides the code above? Perhaps you can point me to some tut on this if there is one ? Much appreciated ..... – user1859 Aug 15 '13 at 13:18
  • Hi! I'm sorry, but I don't know about any tutorial on this topic. – Robson Sobral Aug 15 '13 at 13:38
  • Hi: So do I need to setup any sperate subdirectories / folders? Or is this all that is required ( only changes to config file?) - Talking about option 2 above. Thx,. – user1859 Aug 15 '13 at 13:42
  • Please, forgive me! I was skyping and hit Enter by mistake. Please, look for me on twitter: @robsonsobral. Then, we can talk before I update my answer. – Robson Sobral Aug 15 '13 at 14:07

If your sites are simple in how they use EE templates, you can use a CNAME to map your subdomains to your main domain, and then use some simple redirect rules to route requests to the correct EE templates.

So, say your main site is: www.example.com and you map two subdomains, sub1.example.com and sub2.example.com as CNAMEs. Then, in EE, say you have corresponding template groups:

  1. site (the default, for the www.example.com site)
  2. sub1 (for your sub1.example.com site)
  3. sub2 (for your sub2.example.com site)

Then, in your .htaccess, you could use these kinds of redirects:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub1\.example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/index\.php/sub1/.*$ [NC]
RewriteRule ^(.*)$ /index.php?/sub1/$1 [L]

RewriteCond %{HTTP_HOST} ^sub2\.example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/index\.php/sub2/.*$ [NC]
RewriteRule ^(.*)$ /index.php?/sub2/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

(note that those redirects assume your Apache is using FCGI. If it's using mod_php, you don't need the ? in the RewriteRules -- e.g.,

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

)

So, with the above, a request to www.example.com would map to the default EE template, site/index. A request to sub1.example.com would map to the sub1/index. And a request to sub2.example.com would map to sub2/index.

The more complicated the sites, generally, the more complicated the structure of your EE templates. And, I don't think this approach scales very well, e.g., not having distinct 404 errors for different sites and other possible complications with URLs for the subdomains.

But, if the subdomain sites are, say, just 1-page landing pages, I think this approach works well enough.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.