redirect - 在 nginx 重定向中将未知子域中的下划线替换为破折号

标签 redirect nginx

我收到的网址如下所示:

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/

相关文章:

nginx - 如何配置 nginx 反向代理以使用 SECURE websockets 上游?

python - Uwsgi/Nginx 给我——请求的修改器不可用 : 0 -- Error

bash - Korn Shell 相当于 PIPESTATUS

node.js - res.redirect() cors在MEAN应用程序中不起作用

.htaccess - 在 htaccess 中设置条件重定向

phpMyAdmin "Cannot log in to the MySQL server"与 PHP 7.0.0 RC7

ubuntu - 每天将 Nginx 日志移出服务器是否可能导致损坏?

Apache 和 nginx 的终极配置以正确的方式为所有虚拟主机提供服务

apache - 仅将 SSL 应用于主域 - .htaccess

javascript - 基于美国州的重定向页面,使用 Maxmind geoip2 JS