Magento – Link directly to a specific store view

magento2.2store-view

I'm currently running Magento 2.2.1 and want to create links to a specific store view.

Each store view is translated to a certain language, and I'd like that links placed on blogs written in that language go directly to the respective store view.

I've tried adding the __store=pt query parameter but in Magento 2 this doesn't seem to work. Apparently the store switcher uses a POST call, which I can't do if I'm linking from another site.

Is there something else I can do?

Best Answer

I finally worked this out. It certainly is more complex than it should be but it works.

The problem is that passing the "__store" query param isn't enough for Magento to send the store cookie back. So I do it myself via rewrites.

Instead of using links of the form http://example.com/some/magento/path/?__store=pt I use http://example.com/pt/some/magento/path/.

Then I have an Apache Rewrite rule to redirect to the correct URL (without the pt in the path): http://example.com/some/magento/path/ and send the cookie back.

Here's what that rewrite looks like:

RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=proto:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=proto:http]

RewriteRule ^/pt/(.*) %{ENV:proto}://%{HTTP_HOST}/$1?__store=pt [R,L,QSA,CO=store:pt:;:60:/:HttpOnly]
Related Topic