Question

A client wants his multistore shop to be set up like this:

This is an example but there may be many more different subfolders. What is the best approach to handle different subfolders for different store views / websites?

I know that one solution is to create subfolders called de, en etc. and copy the index.php and .htaccess into the corresponding subfolders.

There also may be some workarounds (symlinks for index.php, vHost configuration instead of using .htaccess) to minimise file duplication but I'd like to find a solution where I don't have to do any changes in the file system but just handle everything by configuration.


UPDATE

We verified with Enterprise support that the best way to do this is by creating subfolders.

We ended up doing it like this:

  • Create a "languagefolders" directory
  • Created a copy of index.php inside the directory along with an adjusted .htaccess and symlinks to the Magento folders (app/, errors/, ...)
  • Created symlinks "de", "en" etc. inside the Magento root directory pointing to the "languagefolders" directory

This way we can add a new language by creating a new symlink (e.g. "fr").

Was it helpful?

Solution

It's very easy to serve multiple domains/paths based on URLs. As mentioned, the easiest setup (configuration-only) is possible when the unique core/store codes can be used in the path as subfolders. This requires one of the following:

  1. Visitors are linked to the correct subfolder path initially
  2. Visitors are served a landing page from which they select their store and receive a cookie
  3. Some mechanism is used to set the run type and run code prior to PHP handling the response

Regarding #3: since 1.4 Magento, made it possible to allow the Web server to determine the running context (website or store) as well as the particular code which should be used. From index.php:

/* Store or website code */
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';

/* Run store or run website */
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';

Mage::run($mageRunCode, $mageRunType);

Whereas environment variables are used to initialize the application, it's possible to influence the system prior to PHP even spinning up. With Apache and mod_rewrite this can be done for subfolders with a bit of trickery:

RewriteCond %{REQUEST_URI} ^/de/$
RewriteRule .* - [E=MAGE_RUN_CODE:deutsch]
RewriteCond %{ENV:REDIRECT_MAGE_RUN_CODE} (.+)
RewriteRule .* - [E=MAGE_RUN_CODE:%1]

Apache is twitchy with environment variables and subfolders, as demonstrated by this excellent SO answer. The initial two lines result in $_SERVER["REDIRECT_MAGE_RUN_CODE"] = 'deutsch'; while the latter two lines provide the needed $_SERVER["MAGE_RUN_CODE"] = 'deutsch';. There are numerous other tricks, but the above has bitten me before.

The end goal should be initial detection as much as is reasonable (geoip + multi-language concerns) while getting the user to set the store cookie which can be used to bypass/step through the logic in subsequent requests.

OTHER TIPS

If the indented URLs (subfolders) can have the same name like the store codes (why not?), you can simply enabled Configuration > Web > Url options > Add Store Code to Urls.

we ended up doing exactly that

We verified with Enterprise support that the best way to do this is by creating subfolders.

We ended up doing it like this:

  • Create a "languagefolders" directory
  • Created a copy of index.php inside the directory along with an adjusted .htaccess and symlinks to the Magento folders (app/, errors/, ...)
  • Created symlinks "de", "en" etc. inside the Magento root directory pointing to the "languagefolders" directory

This way we can add a new language by creating a new symlink (e.g. "fr").

In the backend we set the store base url to domain.com/en

If you use nginx an update is necessary to your location processing to process index.php also in the new sub folder. This needs to be done for every new translation folder

There is an entry in official Magento knowledge base that describes exactly that.

In case you are working with Apache, make sure all Virtual Host entries (for each domain) are pointing to the same document root where your Magento installation lies. That's for multiple domains.

The next (and last) thing is the .htaccess in same Magento installation folder. .htaccess is a powerful directory-level configuration file where you can set rules-actions per each server request. So you will need to handle all different (sub)domains/folders actions there and set the correct server variables that Magento uses at initializing stage.

After you're done with server configuration, you will have to set different base paths for each of the store in Magento admin (system - configuration - general - web).

So make sure you follow the official guide (which is also the preferred way) and things should work smoothly.

First you have to do some configuration in the Magento admin panel, then create a subdirectory, and move the index.php and the .htaccess to the subdirectory, and make some minor changes to the index.php.

Here is a complete example of how to do this.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top