[英]Nginx rewrite - Remove path segments from URL for static assets?
I have the following URL:我有以下 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.我想将其重写为https://example.com/<template>/index.php?v=v1&p1=a&p2=b
,并设法使用下面的正则表达式来做到这一点。
I also have the following static URLs being called from index.php
:我还从index.php
调用了以下 static URL:
https://example.com/<template>/v1/a/b/assets/images/intro.png
How can I redirect all assets
to their proper location?如何将所有assets
重定向到正确位置? 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;
}
}
If the URIs pointing to assets contain the literal word /assets/
, you just need a regular expression that matches that part of the URI.如果指向 assets 的 URI 包含字面词/assets/
,则您只需要一个与 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.正则表达式捕获第一个单词(使用字符 class [^/]
来匹配任何非/
字符),以及从单词assets
到 URI 结尾的所有内容,这两者随后都用于try_files
语句.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.