Multiple domains with single MODx Installation

Multiple domains can run from single MODx installation evolution version. You need to install few tweaks in the core and install some plugin and snippet. There are already few existing ideas mentioned in the MODx Forum. MODx is so flexible and advance that you hardly need to modify any core files but to implement multiple domains we had modified one of the core function of the MODx framework.

Make sure you take backup of the file before making any changes also note that whenever you upgrade the MODx version these can get replaced.

With our approach you will be able to setup any number of domains with single MODx installation and all domains can share common pages also. Make sure you replace “www.mydomain.com”, “www.mydomain.in”, “www-mydomain-com”, “www-mydomain-in” with yours.

Let’s say you want to run 2 domains with single installation:

1. www.mydomain.com (for international customers)
2. www.mydomain.in (for Indian customers)

You need to create site structure like this:
+www.mydomain.com
–index.html
–services.html
–products.html
–about.html
–faq.html
–contact-us.html
–404.html

+www.mydomain.in
–index.html
–services.html
–products.html
–about.html
–faq.html
–contact-us.html
–404.html

common pages shared by both the domain.
links.html
resources.html

1. Enable User-Friendly URLs
First of all you need to enable User-Friendly URLs for your modx installation. Go to Tools >> Configuration >> User-Friendly URLs:

* User-friendly URLs, use: yes
* Suffix:. html
* Use user-friendly alias path information: yes
* Allow duplicate aliases: yes
* Automatically generated aliases: yes

2. Create Sitemap
1st create new document and give it’s title as “www.mydomain.com”, alias as “www-mydomain-com” and deselect show in menu option and create rest of the pages under it.

2nd Create new document document in the root with title “www.mydomain.in” alias as “www-mydomain-in” and show in menu deselected and create rest of the pages under it.

3. Create a plugin
Now you have the site structure ready but the page’s URLs are like following:

www-mydomain-com/index.html
www-mydomain-com/services.html
www-mydomain-in/index.html
www-mydomain-in/services.html

Next step is to create a plugin to remove domain name’s references from the URLs generated by MODx.

1. Go to Admin >> Manage Resource >> Plugins
2. Click “New Plugin” link
3. Enter name as “Muiltisite”
4. In the plugin code, copy the following code and select “OnWebPagePrerender” event for it

global $modx;
 
$my_domains     = array();
$my_domains[]  = "www-mydomain-com";
$my_domains[]  = "www-mydomain-in";
 
foreach( $domains as $domain_alias ){
 
// delete our identification folder from output
$modx->documentOutput=preg_replace("#/?".$domain_alias."/#i","/", $modx->documentOutput);
 
}

4. The real magical hack
Yeah, now time to hack the core to embed the real magical code to detect the domain name and return the pages accordingly.

1. Open “manager/includes/document.parser.class.inc.php”
2. Find following code around line# 1092

// Check use_alias_path and check if $this->virtualDir is set to anything, then parse the path
if ($this->config['use_alias_path'] == 1) {
    $alias= (strlen($this->virtualDir) > 0 ? $this->virtualDir . '/' : '') . $this->documentIdentifier;
    if (array_key_exists($alias, $this->documentListing)) {
        $this->documentIdentifier= $this->documentListing[$alias];
    } else {
        $this->sendErrorPage();
    }
} else {
    $this->documentIdentifier= $this->documentListing[$this->documentIdentifier];
}

3. Replace the following line (around line# 1099 and line# 7 in the above code)

$this->sendErrorPage();

with follwing

$found = false;
 
$my_domains                      = array();
$my_domains['www.mydomain.com']  = "www-mydomain-com";
$my_domains['www.mydomain.in']   = "www-mydomain-in";
 
if (array_key_exists( $my_domains[$_SERVER['HTTP_HOST']]."/".$alias, $this->documentListing)) {
 
  $this->documentIdentifier= $this->documentListing[$my_domains[$_SERVER['HTTP_HOST']]."/".$alias];
  $found = true;
 
}
 
if($found == false){
  $this->sendErrorPage();
}

5. Toggle template folders accordingly
You can create seperate templates for each domain but if you have few pages which are common for each domain and you want to apply different css then you need to create new snippet to toggle between template folders.

In our case we have two skin folders for respective domains:
1. assets/templates/com
2. assets/templates/in

Create a new snippet to return template folder’s name based on domain name.
1. Go to Manager >> Resources >> Manage Resources >> Snippets
2. Click “New Snippet”
3. Enter snippet name as “skin_name”
4. Copy the following code and save

// host name name template folder name mapping
$my_domains                                  = array();
$my_domains['www.mydomain.com']   = "com";
$my_domains['www.mydomain.in']      = "in";
 
return  $my_domains[$_SERVER['HTTP_HOST']];

Now wherever you refer to template folder you need to call the above snippet “skin_name” like this:

<link rel="stylesheet" type="text/css" href="assets/templates/[!skin_name!]/stylesheet.css" />

 

Leave a Comment

We would be glad to get your feedback. Take a moment to comment and tell us what you think.