# How to re-write www.domain1.com to www.domain2.com

**URL:** <https://discourse.haproxy.org/t/how-to-re-write-www-domain1-com-to-www-domain2-com/4063>\
**Category:** Help!\
**Created:** [July 18, 2019, 5:36am UTC](https://discourse.haproxy.org/t/how-to-re-write-www-domain1-com-to-www-domain2-com/4063 "2019-07-18T05:36:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![seema](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@seema](https://discourse.haproxy.org/u/seema)\
**Post date:** [July 18, 2019, 5:36am UTC](https://discourse.haproxy.org/t/how-to-re-write-www-domain1-com-to-www-domain2-com/4063/1 "2019-07-18T05:36:31Z")

</div>

Hi,

We have a requirement to rewrite the domain name from [www.domain1.com](http://www.domain1.com) to [www.domain2.com](http://www.domain2.com) at the haproxy level.

I have tried various options that I found on the internet, but nothing seems to work.

Request inputs from anybody who has done this.

Best Regards,  
Seema

---

<div class="post-metadata">

**Author:** ![ciprian.craciun](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/ciprian.craciun/32/722_2.png) [@ciprian.craciun](https://discourse.haproxy.org/u/ciprian.craciun)\
**Post date:** [July 18, 2019, 6:22am UTC](https://discourse.haproxy.org/t/how-to-re-write-www-domain1-com-to-www-domain2-com/4063/2 "2019-07-18T06:22:02Z")

</div>

Please note that “rewrite” can mean a lot of things when it comes to HTTP. Please be more specific, and give a concrete example in terms of “this is the original browser request, this is how it should actually be reaching the backend server, and this is how the reply body should be changed (if applicable)”.

Do you need to just replace the `Host` request header? If so use `http-request set-header Host www.domain2.com`.

I would also point you to a previous discussion about this topic which discusses a few options:

> [@Redirect to new url but preserve original domain](https://discourse.haproxy.org/t/redirect-to-new-url-but-preserve-original-domain/4021):
>
> I have a [original.com](http://original.com) and wish to redirect to [alias.com](http://alias.com). How do a redirection and keep the URL in the browser.? Is this even possible?

---

<div class="post-metadata">

**Author:** ![seema](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@seema](https://discourse.haproxy.org/u/seema)\
**Post date:** [July 18, 2019, 7:04am UTC](https://discourse.haproxy.org/t/how-to-re-write-www-domain1-com-to-www-domain2-com/4063/3 "2019-07-18T07:04:06Z")

</div>

Hello,  
Thanks for replying.

There is a requirement to access our app using '[newsub.domain2.com](http://newsub.domain2.com)’. So we have configured a CNAME record to forward all requests from '[newsub.domain2.com](http://newsub.domain2.com)’ to [sub.domain1.com](http://sub.domain1.com). The requests reach the LB, but doesn’t work since the application only understands [sub.domain1.com](http://sub.domain1.com).

I was wondering whether we can somehow rewrite the URL from '[newsub.domain2.com](http://newsub.domain2.com)’ to [sub.domain1.com](http://sub.domain1.com) at the haproxy?

Regards,  
Seema

---

<div class="post-metadata">

**Author:** ![ciprian.craciun](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/ciprian.craciun/32/722_2.png) [@ciprian.craciun](https://discourse.haproxy.org/u/ciprian.craciun)\
**Post date:** [July 18, 2019, 10:28am UTC](https://discourse.haproxy.org/t/how-to-re-write-www-domain1-com-to-www-domain2-com/4063/4 "2019-07-18T10:28:50Z")

</div>

First of all, understand that DNS CNAME’s are not redirects. They are just one-way aliases. It’s just like saying \<\<when I say “Deutchland”, I actually mean “Germany”\>\>.

Therefore when your DNS client resolves that `CNAME`, it behaves just like you would have put a normal `A` record with the proper IP.

As hinted before, there isn’t a “one solution” to your problem, but it depends a lot on how your application behaves. Just try that `http-request set-header Host` statement as I’ve previously described. If it works then it should be enough.

However, many web applications “know” that they should run on `sub.domain1.com` (by having this hardcoded in the responses or code), therefore even though from the HTTP protocol point of view they work, they don’t properly work.

I would strongly advise to contact the developers and see if you can configure the application to work properly with the new domain without these “tricks”.

(To be clear: this is not an HAProxy limitation, but of the way the “web” works, especially browsers and web application platforms.)

---

<div class="post-metadata">

**Author:** ![seema](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@seema](https://discourse.haproxy.org/u/seema)\
**Post date:** [July 22, 2019, 5:25am UTC](https://discourse.haproxy.org/t/how-to-re-write-www-domain1-com-to-www-domain2-com/4063/5 "2019-07-22T05:25:08Z")

</div>

Thankyou for the reply.
