Question

I am trying to set Apache 2.4 to serve a new project on Magento 2 (EE).

The structure needs to be:

http://siteA.mydomain.com/     <-- (MAGE_RUN_CODE = aa_uk)
http://siteA.mydomain.com/de/  <-- (MAGE_RUN_CODE = aa_de)
http://siteA.mydomain.com/fr/  <-- (MAGE_RUN_CODE = aa_fr)

http://siteB.mydomain.com/     <-- (MAGE_RUN_CODE = bb_uk)
http://siteB.mydomain.com/de/  <-- (MAGE_RUN_CODE = bb_de)
http://siteB.mydomain.com/fr/  <-- (MAGE_RUN_CODE = bb_fr)

I am trying NOT to use .htaccess (Magento provides some for the various directories, root/, pub/, pub/static/)

The /de/ /fr/ and so on are real directories which only contain a symlink to ../index.php

When I tried using the .htaccess as follows, inside those subdirectories, the pages for each language were working correctly, so for example http://siteA.mydomain.com/de/contact/ was showing correctly with the MAGE_RUN_CODE being set:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^siteA.mydomain.com$
RewriteRule .* - [E=MAGE_RUN_CODE:aa_de]
RewriteCond %{HTTP_HOST} ^siteB.mydomain.com $
RewriteRule .* - [E=MAGE_RUN_CODE:bb_de]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]

The first problem I can't find a solution for is how to tell Apache to rewrite (?) the static assets: by default Magento will try to find all the css and such in http://siteA.mydomain.com/de/static/<subdirectories>/de_AT/mage/<etc>/something.js while they reside inside inside http://siteA.mydomain.com/static/<subdirectories>/de_AT/mage/<etc>/something.js (without /de/). What complicate things is also that while some files do exist, others need to be handled by static.php as shown in the default Magento .htaccess section in /var/www/<magentoroot>/pub/static/.htaccess

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* ../static.php?resource=$0 [L]

Now, trying without .htaccess (I moved them away), setting the following vhost in Apache does not work at all, other than the default store with UK language. I can see MAGE_RUN_CODE being set, I get a 404 even for /de/contact/ This is the relevant part of the vhost that I created using what's in the Magento provided .htaccess files:

    <VirtualHost *:80>                                                                                                                                                       

    DocumentRoot /var/www/html/pub

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^ siteA.mydomain.com $  
    RewriteRule .* - [E=MAGE_RUN_CODE:aa_uk]         
    RewriteCond %{REQUEST_URI} ^/de/                 
    RewriteRule .* - [E=MAGE_RUN_CODE:aa_de]         
    RewriteCond %{ENV:REDIRECT_MAGE_RUN_CODE} (.+)   
    RewriteRule .* - [E=MAGE_RUN_CODE:%1]  

   <Directory /var/www/html/pub/de/>
    AllowOverride None
    Options +FollowSymLinks
    DirectoryIndex index.php

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule .* index.php
    </Directory>

   <Directory /var/www/html/pub/>
        DirectoryIndex index.php
        AllowOverride None
        Options FollowSymLinks

        RewriteEngine on
        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
        RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
        RewriteRule .* - [L,R=405]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-l
        RewriteRule .* index.php [L]

           [...]

    </Directory>

    <Directory /var/www/html/pub/static/>

        Options FollowSymLinks
        php_flag engine 0
        Options -MultiViews

        RewriteEngine On

        RewriteRule ^version.+?/(.+)$ $1 [L]

        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-l
        RewriteRule .* ../static.php?resource=$0 [L]

        [...]

    </Directory>

</VirtualHost>

The questions here being, first: can I not use .htaccess at all or is there some odd reason why it's necessary? I know not everything can be done inside it and not everything can be done inside the vhost configuration.

Second: is there anything wrong in my vhost that is preventing even the basic /de/contact/ page to be shown -let alone the /de/static/ resources not being passed?

Or is it even the whole strategy of sub-directories being wrong?

By the way, this works like a charm using Nginx, with a very simple and elegant configuration:

    location ~ /(ie|fr|de|us)/(static|media) {
         rewrite ie/(.*)$ /$1 last;
         rewrite fr/(.*)$ /$1 last;
         rewrite de/(.*)$ /$1 last;
         rewrite us/(.*)$ /$1 last;
    }
Was it helpful?

Solution

The first problem I can't find a solution for is how to tell Apache to rewrite (?) the static assets

Actually, you don't need to do that. Instead of setting http://siteA.mydomain.com/de/ as Base URL, set it as Base Link URL, and keep the Base URL as http://siteA.mydomain.com/, configuring it in the website level. This will tell Magento to look for assets on your root path, so you don't even need to rewrite the static files, whilst the URLs for products and other links will be generated using /de/.

So you configuration for aa_de should look like this:

enter image description here

(Base URL is configured in the website level, while the Base Link URL is configured in the store level)

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