4

We have hybrid Magento + Typo3 system and want - for Magento - have URLs like that:

de/produkte/ 
en/products/ 
fr/produits/ 
ru/produkty/ 
cn/zhìpǐn/
it/prodotti/ 
es/productos/

Typo3 would overlay the same structure (de/presse + en/press and so on)

What I did is to set the web/(un)secure/base_url to a different URL per store view.

#de
n98-magerun.phar config:set web/unsecure/base_url --scope=stores --scope-id=1 "$BASE"de/produkte/
n98-magerun.phar config:set web/secure/base_url --scope=stores --scope-id=1 "$SECURE_BASE"de/produkte/

#en
n98-magerun.phar config:set web/unsecure/base_url --scope=stores --scope-id=2 "$BASE"en/products/
n98-magerun.phar config:set web/secure/base_url --scope=stores --scope-id=2 "$SECURE_BASE"en/products/

Also I defined in the web-root .htaccess

# URL Rewriting to Magento
RewriteRule ^de/produkte/ mage/index.php [L,env=MAGE_RUN_CODE:de]
RewriteRule ^en/products/ mage/index.php [L,env=MAGE_RUN_CODE:en]
RewriteRule ^fr/produits/ mage/index.php [L,env=MAGE_RUN_CODE:fr]
RewriteRule ^ru/produkty/ mage/index.php [L,env=MAGE_RUN_CODE:ru]
RewriteRule ^cn/zhìpǐn/ mage/index.php [L,env=MAGE_RUN_CODE:cn]
RewriteRule ^it/prodotti/ mage/index.php [L,env=MAGE_RUN_CODE:it]
RewriteRule ^es/productos/ mage/index.php [L,env=MAGE_RUN_CODE:es]

But now in Magento, I only get a page not found from Magento.

As a workaround I rewrote the function \Mage_Core_Controller_Request_Http::getBaseUrl`

public function getBaseUrl()
{
    $url = parent::getBaseUrl();
    $url = str_replace('\\', '/', $url);


    // Workaround Patch
    $parts = explode('/', $this->getRequestUri());
    if ($parts[1] == $_SERVER['MAGE_RUN_CODE']) {
        $url = implode('/', array_slice($parts, 0, 3)) . $url;
    }

    return $url;
}

which removes the specific part from the URL.

While this is working, I am wondering if there is an easier solution? Shouldn't Magento automatically strip away the current base_url ?

4
  • 1
    Just an idea: What if you only set the link base url? Mar 15, 2016 at 13:13
  • @fschmengler What should be the base URL? I used /mage/ - and for the link URL /de/produkte/, but if I open the page, it redirects me to /mage/ - what was the idea behind your comment?
    – Alex
    Mar 15, 2016 at 15:41
  • I thought if the base URL check works after your change, you could set it directly to the shortened version instead and use the link base url to still have the right links generated. Mar 15, 2016 at 15:48
  • It's not about the BaseURL check - it's just that Magento does not seem to remove the base URL from the request URI when parsing, which is strange
    – Alex
    Mar 15, 2016 at 16:24

2 Answers 2

2

The situation is as follows: In a plain Magento where the Magento system is installed to a specific sub folder, the method \Mage_Core_Controller_Request_Http::getBaseUrl calls it's parent and finally \Zend_Controller_Request_Http::setBaseUrl where the detection is done. This checks the $_SERVER['SCRIPT_FILENAME'] which would be in that subfolder. That is why the specific base URL is detected.

In the whole process, the Magento-store-config's base url (web/unsecure/base_url) is not used. And as the detection is based on the script file name, it fails because of using the rewrite.

So it is legit to add an additional base URL logic as done in the question. In the question, kind of an heuristic is used (if the first path part equals to the store code). A more flexible approach might be to set an additional environment variable during the rewriting, for example

RewriteRule ^en/products/ mage/index.php 
    [L,env=MAGE_RUN_CODE:en,env=BASE:en/products/]

and use this "virtual" base later as base URL.

Unfortunately the change to Mage_Core_Controller_Request_Http is not so easy to distribute, see https://stackoverflow.com/questions/9199854/how-do-i-override-mage-core-controller-request-http for more information.

0

Maybe a strange question but if I understand you correct you just want to add the storecodes behind the base url so they can be all be set under one domain. Like the following example:

domain.com/en domain.com/de domain.com/nl

This is a default Magento configuration option (System>Configuration>General>Web and then the first option)

If I understand the question incorrect let me know.

1
  • Almost - but I have two systems integrated: Typo3 + Magento. After the Magento store code, there is a additional part in the URL ("products" translated to the language). That is the problem.
    – Alex
    Mar 16, 2016 at 11:16

Your Answer

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

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