我收到的网址如下所示:
https://some_sub_domain.whatever.com
需要重定向到:
https://some-sub-domain.whatever.com
我不知道子域是什么(它们是用户名)。
虽然我需要替换子域的下划线,但我需要保留其他下划线:
https://some_sub_domain.whatever.com/hey_there_underscore
应该重定向到:
https://some-sub-domain.whatever.com/hey_there_underscore
最佳答案
下面是通过lua重写的方法:
location / {
rewrite_by_lua '
if string.find(ngx.var.host, "_") then
local newHost, n = ngx.re.gsub(ngx.var.host, "_", "-")
ngx.redirect(ngx.var.scheme .. "://" .. newHost .. ngx.var.uri)
end
';
proxy_pass http://my_backend;
proxy_set_header Host $host;
}
关于redirect - 在 nginx 重定向中将未知子域中的下划线替换为破折号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14567634/