我有 2 個這樣的網址:
1 - example.com/user/JohnFifty/
2 - example.com/user/45771/
兩者都有效,但只有一種。我想允許兩者,這是在 PHP 代碼中啟用的。
我試過了
if ($arg_id ~ ([\d] )[^\d]) {
rewrite ^/user/([^/]*)/$ /user.php?id=$1 last;
}
rewrite ^/user/([^/]*)/$ /user.php?username=$1 last;
為了檢測這種情況,如何讓 NGINX 檢測 GET 請求是 ID 還是字串(用戶名)?
謝謝
uj5u.com熱心網友回復:
這正是map
塊非常有用的情況,與命名的捕獲組一起使用:
map $id $id_type {
~^\d $ id;
default username;
}
server {
...
rewrite ^/user/(?<id>[^/] )/$ /user.php?$id_type=$id;
...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/480557.html