5

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

\Magento\Store\Model\Store 

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.

Joel Davey
  • 2,363
  • 1
  • 21
  • 20

2 Answers2

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

Aurélien
  • 400
  • 4
  • 11
  • Thanks for the idea that does work and will accept the answer only downside is I would need physical folders for /en/products that links to /products or am i missing something, I've added my .htaccess – Joel Davey Jul 01 '18 at 19:27
  • No you don't need to have the folder. Base URL is a real folder, base Link URL can be a virtual folder, take a try – Aurélien Jul 02 '18 at 17:14
  • Thanks!, see updated answer, the page is translated e.g /de the menu is in german but I get a 404, the CMS page is set to all store views/global as are all products https://imgur.com/a/jkzIZ9x don't get this if I make the symlink folders – Joel Davey Jul 03 '18 at 09:55
0

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
    …
}
cnst
  • 25,870
  • 6
  • 90
  • 122
  • This doesn't solve the issue of the actual page content generating incorrect links they may well get redirected but that can't be good for SEO @Aurélien has the best answer so far – Joel Davey Jul 02 '18 at 08:35
  • @JoelDavey I'm pretty sure Google is smart enough to figure it out. For SEO, I'd imagine the inbound links from the other websites matter a whole lot more than the internal links, and those would still be correct. – cnst Jul 02 '18 at 15:08