skip to Main Content

We have WordPress in the root / in a physical subfolder /wp and Magento in /products.

We are wanting to make the sites multi-language using sub folders e.g domain.com/en

The problem arises as magento appends the store code (language) after the url so we have

domain.com/en (wordpress)
domain.com/products/en (magento)

Naturally we would like

domain.com/en
domain.com/en/products

Now it’s very easy to make it work with some rewrite rule

RewriteRule ^(.*)/products/?(.*)$ /products/$1 [L]

But still we have an issue as Magento generates the links as /products/en it’s possible to start modifying where these links are generated like in

MagentoStoreModelStore 

In the _updatePathUseStoreView function, this doesn’t seem to handle all links though

In general seems like a bad solution, another idea is to use Apache mod_substitute also seems bad practice, and overhead.

Another option is to have both apps in the root and have some lookup logic to see which url belongs to which app.

Any ideas for a setup that can purely use just Nginx/Apache. That does not compromise on having unique url’s or regex’ing content.

This is my .htaccess in the root

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/wp/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_URI} !^/(.*)/products
RewriteRule ^(.*)$ /wp/$1
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ wp/index.php [L]

RewriteCond %{REQUEST_URI} ^/(.*)/products
RewriteRule ^(.*)$ /products/index.php [L]

</IfModule>

The exact spec I’m trying to achieve is this.

  • WordPress is installed in /wp , Magento in /products
  • Language codes via subfolders used on both sites to appear as /en/wordpress-page /en/products/magento-page

Attempt 1
Use base link URL entering /en/products there and keeping the base URL as /products

as the first request is forwarded I had to work the setEnv like so in the root .htaccess

RewriteCond %{REQUEST_URI} ^/(.*)/products
RewriteRule ^(.*)$ /products/index.php [E=MAGE_RUN_CODE:%1] [L]

then in /products/.htaccess

RewriteCond "%{ENV:REDIRECT_MAGE_RUN_CODE}"
RewriteRule .* - [E=MAGE_RUN_CODE:%{ENV:REDIRECT_MAGE_RUN_CODE}] [L]

I checked the code was coming through on index.php by doing

 echo getenv('MAGE_RUN_CODE');

In my case the store code is “en” etc.. but the language switcher does not work it hits Magento but gets 404 even thought the store code is definitely coming through.

2

Answers


  1. You only need some configuration from backoffice.

    System => Configuration => General => Web => Url options

    Add Store Code to Urls No

    System => Configuration => General => Web => Unsecure

    Base Link URL http://example.com/en/products/

    System => Configuration => General => Web => Secure

    Base Link URL https://example.com/en/products/

    Then, add a rule in htaccess to set the correct store code:


    SetEnvIf Host .*example.com/en* MAGE_RUN_CODE=en_store
    SetEnvIf Host .*example.com/fr* MAGE_RUN_CODE=fr_store

    Login or Signup to reply.
  2. What is the exact spec you’re trying to achieve?

    Do you have multiple pages like /products, and multiple languages like /en?

    I did something similar at BXR.SU — I didn’t like the way OpenGrok, my backend, was handling the URLs, so, I would automatically make my nginx fix the URLs on top of OpenGrok, seamlessly fixing the URLs presented to the user, whereas the backend would continue to use the old URLs (e.g., with the /xref/ for most pages, which I don’t like, and was set to remove with nginx); this approach appears to be similar to your spec, where you want to do this on the front-end web-server without doing any modifications to the backend.

    The approach is briefly described at nginx redirect loop, remove index.php from url, with the idea being that nginx has two types of redirects — internal, where the contents of the $uri variable gets changed (without any visibility to the user), and external, where a 301 Moved (or some such) response is provided to the client (and the user would then see the browser making a request with the new URL).

    E.g., you may want to have something like the following:

    location /en/ {
        # issue an external redirect, unless we're here from an internal one
        if ($request_uri ~ "^(/en)(/product)(.*)") {
            return 301 $2$1$3; # external redirect
        }
        proxy_pass …;
    }
    location /products/ {
        rewrite ^(/products/)(en/)(.*) $2$1$3 last; # internal redirect
        …
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search