我在設定 NGINX 時遇到了一些問題,以便所有帶有斜杠 (/) 的 URL 永久重定向到沒有斜杠的變體。問題在于,除了以/backend.
例如:
https://example.com/service/ --> https://example.com/service
https://example.com/service/one/ --> https://example.com/service/one
https://example.com/backend/ --> https://example.com/backend/ (should not redirect)
我目前正在使用這個指令:
server {
listen 80;
listen 443 ssl http2;
[...]
location ~ ^/(?!backend)(. )/$ {
return 301 $1$is_args$args;
}
[...]
}
不幸的是,這里發生以下錯誤:
https://example.com/service/ --> https://example.com/service/service
有人可以幫我解開這個正則運算式之謎嗎?
uj5u.com熱心網友回復:
您的return 301 $1$is_args$args;運算式缺少前導,/因此瀏覽器會將重定向解釋為相對于當前 URI。service相對/service/的/service/service。
無論是捕捉龍頭/的$1或明確添加到return陳述句。
例如:
location ~ ^/(?!backend)(. )/$ {
return 301 /$1$is_args$args;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/365425.html
上一篇:將Row中的專案垂直居中
