Hi all.
I have a lot of rewrite rules (coming from Prestashop) that works as expected if the shop is located in the root path (ie: “/”).
These rules are stored in a snippet file so that I can include them in all domains by simply including a single config file.
Now, I have an issue: some prestashop installs are under a sub-folder (ie: ‘/shop’) and thus the rewrite regex doesn’t match anything, in example: rewrite ^/(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$1$2.jpg last;
I’ve thought to set a variable, called $shop_base_url and set to ‘/’ as default, overridden in each website config if the shop is in una subdirectory, but I can’t use the variable in the regex.
Any idea ?
1 Like
you should be able to use variables in the rewrite definition, you just have to make sure it is being set before the rewrite is hit. Do you have an example of the configurations that you have tried and did not work as expected?
Do you have an example of the configurations that you have tried and did not work as expected?
This is something i’m trying:
This is in the virtualhost
set $prestashop_base_url "/shop/";
include snippets/prestashop.conf;
This is the snippet prestashop.conf
that i’m including in each virtualhost with presatshop:
if ( $prestashop_base_url = "" ) {
set $prestashop_base_url "/";
}
location $prestashop_base_url {
rewrite ^${prestashop_base_url}(\d)(\d)(-[\w-]+)?/.+\.jpg$ ${prestashop_base_url}img/p/$1/$2/$1$2$3.jpg last;
try_files $uri $uri/ ${prestashop_base_url}/index.php?$query_string;
}
We have multiple CMSs on this website, there is Laravel install on /
and a prestashop install on /shop
Both have their own rewrite rules, so on Laravel, I have to “filter-out” anything related to prestashop. (how?)
Physical path for prestashop is different than the URL: shop is physically located in /public/shop
but url is /shop
so probably the try_files
has to be changed in some way.
Any idea ?
For filtering out the prestashop related assets, if there are references in the HTML that are trying to load assets that are not using the correct /shop path, you can use the sub_filter directive to rewrite those on the fly.
For the physical path part, does the config use proxy_pass to route the requests to the prestashop app? If it does, the path can be added on in that space. If it is a location on the filesystem, you can define a root directive in the location block to server data from a specific path on the system.
I’m not an nginx expert, i use it from less than a year.
This is my full virtualhost config (all virtualhosts are the same), could you please give me a working example?
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name mydomain.com;
ssl_certificate /var/www/$server_name/ssl/fullchain.cer;
ssl_certificate_key /var/www/$server_name/ssl/certificate.key;
access_log /var/log/nginx/mydomain.com/access.log;
error_log /var/log/nginx/mydomain.com/error.log;
root /var/www/mydomain.com/web/public;
index index.php index.html index.htm;
set $prestashop_base_url "/shop/";
include snippets/prestashop.conf
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
fastcgi_pass unix:/run/php/mydomain.com.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
}
The snippet (has to be fixed with the changes requested in this thread):
if ( $prestashop_base_url = "" ) {
set $prestashop_base_url "/";
}
rewrite ^/(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$1$2.jpg last;
rewrite ^/(\d)(-[\w-]+)?/.+\.webp$ /img/p/$1/$1$2.webp last;
rewrite ^/(\d)(-[\w-]+)?/.+\.avif$ /img/p/$1/$1$2.avif last;
rewrite ^/(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$1$2$3.jpg last;
rewrite ^/(\d)(\d)(-[\w-]+)?/.+\.webp$ /img/p/$1/$2/$1$2$3.webp last;
rewrite ^/(\d)(\d)(-[\w-]+)?/.+\.avif$ /img/p/$1/$2/$1$2$3.avif last;
so is the primary concern here that the rewrites are sent /shop/img instead of /img? From looking at the prestashop docs you are missing a few location blocks to serve the files from. Does the default config work as expected, you just want to sometimes serve the static files from a different directory?
the rewrite config is much longer, i’ve posted just some test lines
standard config with PrestaShop in the root dir works, my issue is sharing the same config file (with some adjustments) allowing multiple PrestaShop installs in subdirs, in example:
/
laravel install
/shop
PrestaShop1
/otherstore
PrestaShop2
and so on.