0

I'm looking for a method to redirect visitors from https://site1.co.uk to https://site2.co.uk in HAProxy. I also need to redirect sub-pages (i.e. https://site1.co.uk/page1) to similar pages on site2.co.uk but using specific URLs. I have stuck a sample below of what I'd like to achieve if that helps...

Home redirect: https://site1.co.uk -> https://site2.co.uk

Sub redirect: https://site1.co.uk/first-page.html -> https://site2.co.uk/about-us.html

I've been searching for the last day or so for a simple solution to this problem and have been unable to find anything that does the trick.

I'm using HAProxy 1.5 (version required for a specific project) and any help would be much appreciated!

UPDATE

Things I've tried...had to remove HTTPS www.'s due to account limit.

redirect prefix site2.org.uk/subpage2/endresult2.html if { hdr(host) -i site1.org.uk }

redirect prefix site2.org.uk/subpage2/endresult2.html if { hdr(host) -i site1.org.uk/site1page.html }

Line 1: redirects correctly.

Line 2: End result is site2.org.uk/subpage2/endresult2/site1page.html - this is incorrect and should not include the site1page.html page on the end.

End result should be site2.org.uk/subpage2/endresult2 - this is how it I would like it to appear.

The server that HAProxy is on also serves multiple sites with different URLs. The above code is also redirecting them yet it shouldn't interact. How can I prevent this from happening?

UPDATE 2

I've noticed that when I enable a redirect, it's affecting my other sites which route through HAProxy and not just the one I'm trying to redirect (i.e. site1)

2 Answers2

0

if you need to redirect your URL, you can check below link:

How to redirect URL with HAProxy

in haproxy you should use configuration like above

Elham_Jahani
  • 369
  • 2
  • 8
  • Thanks for the reply! Unable to get that to work unfortunately... [ALERT] 126/075845 (128576) : parsing [/etc/haproxy/haproxy.cfg:80] : error detected in frontend 'fb-http' while parsing redirect rule : expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '}'). [ALERT] 126/075845 (128576) : Error(s) found in configuration file : /etc/haproxy/haproxy.cfg [ALERT] 126/075845 (128576) : Fatal errors found in configuration. line added: 'redirect prefix http://www.site2.org.uk } if { hdr(host) -i http://site1.org.uk }' – thedaishboy May 07 '19 at 07:59
  • might be me...seems there is a rouge '}' just after the first URL...is there an easy way to test these additions to the code before service restart to confirm everything is working as expected? I know with apache redirects you can test them using a tool online? – thedaishboy May 07 '19 at 08:04
  • you can test your config file and validate it with this command: – Elham_Jahani May 07 '19 at 08:49
  • haproxy -c -V -f /etc/haproxy/haproxy.cfg – Elham_Jahani May 07 '19 at 08:49
  • can you write your config file ? what is the "}" before "if" in your line that added in config file. – Elham_Jahani May 07 '19 at 08:52
  • change "redirect prefix site2.org.uk } if { hdr(host) -i site1.org.uk } " to "redirect prefix site2.org.uk if { hdr(host) -i site1.org.uk }" – Elham_Jahani May 07 '19 at 08:55
  • updated my question above with the information. Any help would be appreciated? – thedaishboy May 07 '19 at 14:18
  • redirect prefix site2.co.uk/about-us.html if { hdr(host) -i site1.co.uk } { url_beg /first-page.html } – Elham_Jahani May 08 '19 at 04:49
  • please test above config and tell me what happened – Elham_Jahani May 08 '19 at 04:50
  • Thank you for this! Will test it out shortly...also how can I tell the server to only redirect attempts to access site1 etc? If I have another site in HAProxy (site3 say) and it is also redirecting traffic from site3 to site2 using the site1 redirect. How can I force it to just redirect site1 traffic please? – thedaishboy May 08 '19 at 07:28
  • Hi, I am getting a problem where the redirects are also redirecting one of my other domains hosted on the server. Do you know how I can force the redirects to only direct traffic from site1? – thedaishboy May 08 '19 at 23:22
  • https://stackoverflow.com/questions/62454041/haproxy-redirect-to-new-domain-if-string-found-in-request-but-keep-and-send-all this might be useful – shantanoo Oct 06 '22 at 07:29
0

Although not technically the answer I wanted...I realised that I could redirect using the httpd configuration file instead of HAProxy. In the end that was the easiest option for a newbie like myself. Time will tell if this performs well...