I've got URLs coming in that look like this:
https://some_sub_domain.whatever.com
That need to be redirected to:
https://some-sub-domain.whatever.com
I don't know what the subdomains will be (they're usernames).
While I need to replace underscores for the subdomain, I need to leave other underscores in-tact:
https://some_sub_domain.whatever.com/hey_there_underscore
Should redirect to:
https://some-sub-domain.whatever.com/hey_there_underscore
Here's a way to rewrite via 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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With