

Blog
Published On - February 21, 2019
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. 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:
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:
Next step is to create a plugin to remove domain name’s references from the URLs generated by MODx.
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.
// 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];
}
$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:
Create a new snippet to return template folder’s name based on domain name.
// 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” />
Tags:
Leading software development company. We specialize in web and mobile application development.
We are strategists. We are innovators. We are a team of full-stack enterprise and mobile app developers
Office Address
B-680, 2nd Floor,
Green Field Colony, Faridabad
121010
Haryana, India
Phone: +91 93133 54455
