Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[magento] multidomain-multistore-multistoreview setup with visible language codes

Use these snippets to setup a magento installation with the following structure:

No additional htaccess/apache rewrites required! Make shure you stick to the naming convention mentioned above under System/Manage Stores! Note: A more reliable approach (especially when using proxies/load-balancers e.g. in a setup with varnish upfront) would be to make use of the environment variables in your nginx/apache/php-fpm vhosts and switch them accordingly

fastcgi_param MAGE_RUN_TYPE store;
fastcgi_param MAGE_RUN_CODE domain1_en;

For this to work properly, you also have to manually adjust the settings at

  • website: domain / storeview: domain_en / System/Configuration/Web/Unsecure: Base Link Url -> {{unsecure_base_url}}en/
  • website: domain / storeview: domain_en / System/Configuration/Web/Secure: Base Link Url -> {{secure_base_url}}en/
  • website: domain / storeview: domain_de / System/Configuration/Web/Unsecure: Base Link Url -> {{unsecure_base_url}}de/
  • website: domain / storeview: domain_de / System/Configuration/Web/Secure: Base Link Url -> {{secure_base_url}}de/
  • website: domain1 / storeview: domain1_en / System/Configuration/Web/Unsecure: Base Link Url -> {{unsecure_base_url}}en/
  • website: domain1 / storeview: domain1_en / System/Configuration/Web/Secure: Base Link Url -> {{secure_base_url}}en/
  • website: domain1 / storeview: domain1_de / System/Configuration/Web/Unsecure: Base Link Url -> {{unsecure_base_url}}de/
  • website: domain1 / storeview: domain1_de / System/Configuration/Web/Secure: Base Link Url -> {{secure_base_url}}de/
  • website: domain2 / storeview: domain2_en / System/Configuration/Web/Unsecure: Base Link Url -> {{unsecure_base_url}}en/
  • website: domain2 / storeview: domain2_en / System/Configuration/Web/Secure: Base Link Url -> {{secure_base_url}}en/
  • website: domain2 / storeview: domain2_de / System/Configuration/Web/Unsecure: Base Link Url -> {{unsecure_base_url}}de/
  • website: domain2 / storeview: domain2_de / System/Configuration/Web/Secure: Base Link Url -> {{secure_base_url}}de/
<?php
include_once $_SERVER['DOCUMENT_ROOT'].'/storemapping.php';
# replace the REQUEST_URI such as /en/blah.html with /blah.html
$_SERVER['REQUEST_URI'] = preg_replace("#^/$lang(/.*)#i",'$1',$_SERVER['REQUEST_URI']);
# we want our /lang/ urls to take priority over store querystrings
# but not if we are in the admin panel
if ( ! preg_match('#^/index.php/admin/#i',$_SERVER['REQUEST_URI']) ) {
$_GET['___store'] = $store;
}
umask(0);
Mage::run($store,'store');
<?php if(count($this->getStores())>1): ?>
<div class="store-switch">
<?php foreach ($this->getStores() as $_lang): ?>
<?php $_selected = ($_lang->getId() == $this->getCurrentStoreId()) ? ' active' : 'inactive' ?>
<?php
if ( preg_match('#_de$#i',$_lang->getCode()) ) {
$lang = 'de';
} else if ( preg_match('#_en$#i',$_lang->getCode()) ) {
$lang = 'en';
} else {
$lang = 'de';
}
?>
<li class="<?php echo $lang ?><?php echo $_selected ?>"><a href="/<?php echo $lang ?>" title="<?php echo $this->htmlEscape($_lang->getName()) ?>" class="store_<?php echo $_selected ?>" id="store_<?php echo $_lang->getId(); ?>" data-language="<?php echo $lang ?>"><?php echo $lang ?></a></li>
<?php if ($lang == 'de'){
echo ' / ';
} ?>
<?php endforeach; ?>
<script>
jQuery(document).ready(function() {
jQuery('.store-switch a').on('click', function(e){
e.preventDefault();
var currentLanguage = jQuery("html").attr("lang");
var newLanguage = jQuery(this).data("language");
var url = window.location.pathname.toString();
var newUrl = url.replace("\/"+currentLanguage, "\/"+newLanguage);
window.location.href = window.location.protocol + "//" + window.location.host + newUrl
});
});
</script>
</div>
<?php endif; ?>
<?php
if ( strpos($_SERVER['HTTP_HOST'], "www.domain1.tld") !== false ) {
$store='store1';
} else if( strpos($_SERVER['HTTP_HOST'], "www.domain2.tld") !== false ) {
$store='store2';
} else {
$store='store'; # which is the default store of your installation (www.domain.tld)
}
if ( strpos($_SERVER['REQUEST_URI'], "/de/") !== false ) {
$lang = 'de';
} else if ( strpos($_SERVER['REQUEST_URI'], "/es/") !== false ) {
$lang = 'es';
} else {
$lang = 'en'; # which is the default language of the default store of your installation (/)
}
$store .= '_' . $lang;
@dpivato

This comment has been minimized.

Copy link

dpivato commented Oct 17, 2018

Useful code, any chance you could provide a similar snippet for Magento 2?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.