Redirect www to non-www and http to https in one redirect? - haproxy

I've been trying to get this to work, but cannot.
I know I can redirect http -> https using something like this:
http-request redirect scheme https code 301 if !{ ssl_fc }
I know I can redirect www -> non-www using something like this:
http-request redirect prefix https://%[hdr(host)] code 301 if { hdr_beg(host) -i www }
// although note this doesn't work for me, it does nothing
I need the following:
http://example.com/any-uri -> https://example.com/any-uri
http://www.example.com/any-uri -> https://example.com/any-uri
https://www.example.com/any-uri -> https://example.com/any-uri
How can I do both http -> https and www -> non-www using a single 301 redirect?
Thanks

I have a solution, although it requires two rules.
http-request redirect prefix https://%[hdr(host),regsub(^www\.,,i)] code 301 if { hdr_beg(host) -i www. }
http-request redirect scheme https code 301 if !{ ssl_fc }
Based on the above, the following will happen, all with a single redirect:
http://example.com/any-uri -> https://example.com/any-uri
http://www.example.com/any-uri -> https://example.com/any-uri
https://www.example.com/any-uri -> https://example.com/any-uri
This is because the first rule catches http://www... and https://www... URLs and redirects them to https://example.com. The remaining case is http://example.com which gets handled by the second rule via a simple redirect to https://example.com.

Related

How to redirect / forward domain (including subdomain and routes)

I have two domains: example.com and example.net. I want to redirect / forward every thing from example.com to example.net, for example:
mysite.com => example.net
api.mysite.com => api.example.net
mysite.com/news => example.net/news
How can I do this?
It's very easy on NGINX.
server {
listen 80;
server_name ~^(?<subdomain>\w+)\.example\.com$;
return 301 $scheme://$subdomain.example.net$request_uri;
}
You can use whatever redirect code you want instead of 301. You can read about redirect codes from this post.

HAproxy redirect exact URL

Hi i need redirect only www.example.com/foo in foo.example.com, if try in this way:
redirect location http://foo.example.com code 301 if { path_beg /foo }
all /foo (include foo.example.com/foo) redirect to wrong URL, how can redirect only www.example.com/foo?
Thanks

haproxy redirect both scheme and location together

I need to redirect a specific http URL first to its equivalent https and then on to a completely different https URL (don't ask why I can't just redirect the original http straight to the final https URL, this is what the client wants and the client is always right!). Additionally I need to be able to redirect the original https to the different https too.
So, what I need is the ability to redirect http://foo.bar.com => https://foo.bar.com and then https://foo.bar.com => https://another.foobar.com, as well as redirecting https://foo.bar.com => https://another.foobar.com.
Currently to redirect just https://foo.bar.com => https://another.foobar.com I'm using this:
acl is_new_portal hdr(host) -i foo.bar.com
redirect location https://another.foobar.com code 302 if is_new_portal
with an initial bind on port 443, and I know to redirect http to https I would use:
redirect scheme https code 302 if !{ ssl_fc }
(using code 302 rather than 301 as eventually another.foobar.com will be removed, so I don't want the redirection permanently cached in clients' browsers)
but I need to be able to do both relocations, and I'm not sure how you combine the two?
I am not certain to understand if your issue is related to the binding or to the ACLs. You already have all the answers to your question. You can wrap them in a simple frontend:
frontend main
bind :80
bind :443 ssl crt yourCertFile
acl is_new_portal hdr(host) -i foo.bar.com
redirect scheme https code 302 if !{ ssl_fc } is_new_portal
redirect location https://another.foobar.com code 302 if { ssl_fc } is_new_portal
http-response set-header Strict-Transport-Security max-age=31536000;\ includeSubDomains;\ preload; if { ssl_fc }
The space between ACLs after the if is interpreted as a AND. So you will get something like:
redirect to https IF the host is foo.bar.com AND NOT using ssl
redirect to https://another.foobar.com IF the host is foo.bar.com AND using ssl

redirect 301 multiple domain name haproxy

I have a lot of domain names (example.com, www.example.com, example.net, www.example.net, etc...).
How can I redirect all these domains with haproxy ?
For the moment, I am able to redirect domain name by domain name with :
redirect prefix http://www.example.com code 301 if { hdr(host) -i example.fr }
redirect prefix http://www.example.com code 301 if { hdr(host) -i www.example.fr }
But I'd like to have just one line with all my domain names...
Regards
Try an if statement that checks if the host is not www.example.com
redirect prefix http://www.example.com code 301 if !{ hdr(host) -i www.example.com }

Nginx redirect domain.com/blog/posts to sub.domain.com/blog/posts

Yet another nginx redirect question.
I've been trying to redirect domain.com/blog/post-1, /blog/post-2 to sub.domain.com/blog/post-1, etc.
Any pointers?
If you don't want to serve anything on domain.com without subdomains, add this block:
server {
server_name domain.com;
return 301 $scheme://sub.domain.com$request_uri;
}
If you want to use it somehow, add this to your domain.com server block:
location /blog {
rewrite ^ http://sub.domain.com$request_uri? permanent;
}
Of course, in any case you want sub.domain.com server block which is catching this request.