30

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").

6
  • We did just this only to find that the website cannot find any product or category (Magento 404 / no route to page). Did you encounter this also?
    – snh_nl
    Commented Apr 24, 2015 at 14:04
  • Not as far as I remember, sorry. Did you re-create all indexes? Commented Apr 27, 2015 at 8:38
  • We use nginx and an update is necessary to the conf to make it work. I will update my comment
    – snh_nl
    Commented Apr 27, 2015 at 12:09
  • 2 more strange things. 1) /checkout returns a 404 and 2) the language chooser: if I click english, then click english again we see that the uri keeps adding /en/en/en/etc .... what did you choose as base url? domain.com/en or didi you keep it as domain.com
    – snh_nl
    Commented Apr 30, 2015 at 11:08
  • We chose domain.com/en/. Commented May 4, 2015 at 18:01

5 Answers 5

18

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.

7
  • 1
    Thanks, Ben. Doing it this way, I can initialise the store views but I always get the Magento 404 page ("Whoops, our bad"). I set the base URL to domain1.org/en, domain.org/de and so on. Skin URL, Media URL etc. are set to domain.org/(skin|media|...) Please note that the correct pages are returned if I use the normal "copy index.php and .htaccess" approach. Commented Jan 27, 2013 at 18:06
  • I accepted this answer as it gets the closest to what we tried to accomplish (even when we solved it a little bit different for our special requirements). Commented Sep 14, 2013 at 15:29
  • Really surprised that "Add Store Code to URL" setting didn't work.
    – benmarks
    Commented Sep 15, 2013 at 11:10
  • Store codes have to be unique. How should it work with several store views all ending with de/, fr/ or en/? Commented Sep 15, 2013 at 20:23
  • I think I missed the replicated store codes, though I think I was assuming some Web server-based rewriting (e.g. mode_rewrite) based on HTTP_HOST + subfolder level 1.
    – benmarks
    Commented Sep 15, 2013 at 20:36
9

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.

5
  • Correct, then you will not even need subfolders. as long as url rewriting is enabled, Magento will know that "en" is a storeview code and not look for a subfolder. Commented Jan 25, 2013 at 9:48
  • 8
    The problem with this solution is, that Magento allows only one store code "de" in a Magento installation. So if you have several websites with the same languages you have to use different storecodes, e.g. de_de, de_en, ...
    – therouv
    Commented Jan 25, 2013 at 10:13
  • 2
    Rouven got it. The problem is that you cannot have multiple stores with the same store code. Commented Jan 25, 2013 at 10:23
  • Yes, the problem is that - as Rouven said - you cannot have two store views using the same store code. Commented Jan 25, 2013 at 13:22
  • I confirm that Rouven got it right. A bug was reported to Magento. Because in these modern environments with more multistores this should work. However, do you think it would be possible to make the setting Add store codes to url a storeview setting? magento.stackexchange.com/questions/60686/… = suppose there 1 multistore with sep domains, the other uses /en and /fr
    – snh_nl
    Commented Apr 23, 2015 at 14:03
3

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

2
  • snh_nl Is there a chance you explain what your last 2 lines mean? I m having the same problem you said before but I can't understand: 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. I m using nginx and I m trying to do the same. Thanks
    – vbak
    Commented Sep 3, 2016 at 8:58
  • location @rewrite { rewrite /(../)? /$1index.php; }
    – snh_nl
    Commented Sep 3, 2016 at 12:26
2

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.

4
  • 1
    The question was not about multiple domains. Commented Jan 25, 2013 at 8:54
  • Question was about multistore setup WITH MULTIPLE DOMAINS (.org and .ch are different domains). Not that it matters though, because the provided answer describes a solution to multistore setup regardless if you have multiple domains, subdomains or just different subfolders for each of your stores. Commented Jan 25, 2013 at 9:05
  • 1
    But the focus was on paths for the store views, it did not sound like the domains were a problem. Anyway, after your edit I changed my vote. Commented Jan 25, 2013 at 9:14
  • 1
    Exactly, the problem isn't that multiple domains have to be used but that several stores must share the same path / "subfolder" which isn't possible by adding store codes to the URL. Commented Jan 25, 2013 at 13:22
-2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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