Allen King
Allen King

Reputation: 2506

HAProxy how to detect url is IP address instead of domain name

I want to redirect to http://domainname if user types http://ip address. What would be the frontend rule?

For example, this rule can find out if www is prefixed to a domain and if not redirect to www.domain.com.

http-request redirect code 301 location      \
  http://www.%[hdr(host)]%[capture.req.uri]  \
  unless { hdr_beg(host) -i www }

Similarly, I want to redirect to www.domain.com when user types IP address in browser.

Upvotes: 2

Views: 2997

Answers (2)

Drew
Drew

Reputation: 195

You can check the Host header value to see if it matches a regular expression that's roughly an IP address, something like:

acl ACL_IS_IP hdr(host) -i -m reg (\d+)\.(\d+)\.(\d+)\.(\d+)
http-request redirect code 301 location https://www.example.com/ if ACL_IS_IP

Upvotes: 0

nuster cache server
nuster cache server

Reputation: 1791

Since user types IP address in browser, this may work:

 http-request redirect code 301 location http://www.example.com%[capture.req.uri] if { hdr_reg(host) -i 127.0.0.1|192.168.1.100 }`

but it fails if user specifies Host: example.com

Upvotes: 3

Related Questions