Reputation: 681
I have the following URL:
https://example.com/<template>/v1/a/b
I wanted to rewrite it to https://example.com/<template>/index.php?v=v1&p1=a&p2=b
, and managed to do so using the regex below.
I also have the following static URLs being called from index.php
:
https://example.com/<template>/v1/a/b/assets/images/intro.png
How can I redirect all assets
to their proper location? which is:
https://example.com/<template>/assets/...
Here is my setup so far:
server {
root /var/www/html;
index index.html index.php;
server_name _;
location / {
try_files $uri $uri/ @rewrites;
}
location @rewrites {
rewrite ^(.*)/(.*)/(.*)/(.*)/?$ $1/index.php?a=$2&b=$3 last;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.0-fpm.sock;
}
}
Upvotes: 1
Views: 1403
Reputation: 49692
If the URIs pointing to assets contain the literal word /assets/
, you just need a regular expression that matches that part of the URI.
For example:
location ~ ^(/[^/]+)(/.*)?(/assets/.*)$ {
try_files $1$3 =404;
}
The regular expression captures the first word (using the character class [^/]
to match anything which is not a /
character), and everything from the word assets
to the end of the URI, both of which are subsequently used in the try_files
statement.
Upvotes: 1