# Redirect based on hostname

**URL:** <https://www.ruby-forum.com/t/redirect-based-on-hostname/204860>\
**Category:** NGINX\
**Created:** [March 23, 2011, 1:41pm UTC](https://www.ruby-forum.com/t/redirect-based-on-hostname/204860 "2011-03-23T13:41:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [March 23, 2011, 1:41pm UTC](https://www.ruby-forum.com/t/redirect-based-on-hostname/204860/1 "2011-03-23T13:41:07Z")

</div>

I’ve read the whole NginX wiki and couldn’t find a solution yet. So  
would you be so gentle to tell me how to do this.

I have two (2) domains on the same website. Let’s say “[www.first.com](http://www.first.com)”  
and “[www.second.com](http://www.second.com)”  
The frontend php shares the same files and the only difference is in the  
layout so I must differentiate the layout based on the hostname header  
sent by client.

My goal would be to redirect to the www domain if an invalid subdomain  
is typed.

example: [error.first.com](http://error.first.com) must redirect to [www.first.com](http://www.first.com) as well as  
[wrong.second.com](http://wrong.second.com) must redirect to [www.second.com](http://www.second.com)

My setup when I was using a single domain was easy:

```
    if ($host !~* "www.first.com") {
        rewrite ^(.*)$ http://www.first.com$1 permanent;
        break;
        }

```

but now my need is to do something like:

```
    if ($host !~* "www.first.com|www.second.com") {
        rewrite ^(.*)$ (what to put here???)$1 permanent;
        break;
        }

```

Thanks to anyone able to help me out with this issue…

Posted at Nginx Forum:

> **[redirect based on hostname](https://forum.nginx.org/read.php?2,185113,185113#msg-185113)**
>
> I've read the whole NginX wiki and couldn't find a solution yet. So would you be so gentle to tell me how to do this. I have two (2) domains on the same website. Let's say "www.first.com" and "www.second.com" The frontend php...

---

<div class="post-metadata">

**Author:** ![sevy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@sevy](https://www.ruby-forum.com/u/sevy)\
**Post date:** [March 23, 2011, 1:44pm UTC](https://www.ruby-forum.com/t/redirect-based-on-hostname/204860/2 "2011-03-23T13:44:15Z")

</div>

On 23.03.2011, at 15:40, sevy wrote:

> My goal would be to redirect to the www domain if an invalid subdomain  
> break;
> 
> Thanks to anyone able to help me out with this issue…

> **[Converting rewrite rules](http://nginx.org/en/docs/http/converting_rewrite_rules.html)**

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![sevy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@sevy](https://www.ruby-forum.com/u/sevy)\
**Post date:** [March 23, 2011, 1:48pm UTC](https://www.ruby-forum.com/t/redirect-based-on-hostname/204860/3 "2011-03-23T13:48:01Z")

</div>

Thanks Igor, you are always the best!  
And it’s pretty obvious when speaking with the programmer of the best  
http server of the planet 🙂

Posted at Nginx Forum:

> **[Re: redirect based on hostname](https://forum.nginx.org/read.php?2,185113,185115#msg-185115)**
>
> I've read the whole NginX wiki and couldn't find a solution yet. So would you be so gentle to tell me how to do this. I have two (2) domains on the same website. Let's say "www.first.com" and "www.second.com" The frontend php...
