Prestashop 1.7.8.6 multistore nginx rewrite rules

583 views Asked by At

I install the prestashop 1.7 as a multistore and write nginx rewrite rule as

location /shop-1/ {
  rewrite ^/shop-1/(.*)$ /$1 last;
  try_files $uri $uri/ /index.php?$args;
}
location /shop-2/ {
  rewrite ^/shop-2/(.*)$ /$1 last;
  try_files $uri $uri/ /index.php?$args;
}

I follow the nginx conf file from https://devdocs.prestashop.com/1.7/basics/installation/nginx/

Now the Q is Parent domain navigating correctly and showing images but multistore redirected corrected to http://example.com/{shop-1 or shop-2} but not showing the images on the multishop urls, getting nginx 404 error on multishop url but same image showing on a parent domain.

example:

http://example.com/shop-1/45-medium_default/skirt.jpg not showing the image

http://example.com/45-medium_default/skirt.jpg showing the image

1

There are 1 answers

2
Ivan Shatsky On

To explain what happened here I need to refer to nginx request processing phases, a subject not many people actually understand correctly.

Here is a set of rewrite rules being executed at the NGX_HTTP_SERVER_REWRITE_PHASE:

rewrite ^/(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$1$2.jpg last;
rewrite ^/(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$1$2$3.jpg last;
rewrite ^/(\d)(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$3/$1$2$3$4.jpg last;
rewrite ^/(\d)(\d)(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5.jpg last;
rewrite ^/(\d)(\d)(\d)(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6.jpg last;
rewrite ^/(\d)(\d)(\d)(\d)(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7.jpg last;
rewrite ^/(\d)(\d)(\d)(\d)(\d)(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8.jpg last;
rewrite ^/(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9.jpg last;
rewrite ^/c/([\w.-]+)/.+\.jpg$ /img/c/$1.jpg last;

As you can see, none of them matches the /shop-1/45-medium_default/skirt.jpg request URI. So on the next turn, during the NGX_HTTP_FIND_CONFIG_PHASE your location /shop-1/ { ... } will be selected to handle the request. Next, after rewrite ^/shop-1/(.*)$ /$1 last; rule being executed, your request URI will be rewritten to /45-medium_default/skirt.jpg, and due to the used last flag on the rewrite directive the NGX_HTTP_FIND_CONFIG_PHASE will be executed again. However the NGX_HTTP_SERVER_REWRITE_PHASE won't be executed again, and a new URI won't be rewritten according to those rules. What you should do instead is to place a rewrite rule at the server level before the ruleset for rewriting image requests:

rewrite ^/(?:shop-1|shop-2)(/.*) $1;
... image URIs rewrite rules here

Note that I don't use last (or break) flag for this rule since the rewrite rules chain should not be terminated after this rewrite will be triggered. None of those two locations that you show in your question will be needed at all.