# Redirect foo.com to www.foo.com

**URL:** <https://www.ruby-forum.com/t/redirect-foo-com-to-www-foo-com/115744>\
**Category:** Deployment\
**Created:** [September 28, 2007, 5:19am UTC](https://www.ruby-forum.com/t/redirect-foo-com-to-www-foo-com/115744 "2007-09-28T05:19:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [September 28, 2007, 5:19am UTC](https://www.ruby-forum.com/t/redirect-foo-com-to-www-foo-com/115744/1 "2007-09-28T05:19:20Z")

</div>

Is there an easy way to redirect [foo.com](http://foo.com) to [www.foo.com](http://www.foo.com) in nginx?  
Either that or if somebody knows a way to get restful authentication to  
store a cookie for either [www.foo.com](http://www.foo.com) or [foo.com](http://foo.com) that would be great.  
My current cookie is stored with this line:

cookies[:auth\_token] = { :value =\>  
self.current\_user.remember\_token,:expires =\>  
self.current\_user.remember\_token\_expires\_at }

I tried adding :domain =\> ‘.foo.com’ but that didn’t seem to work. I  
figure the nginx redirect would be the easiest thing…

Thanks.

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [September 28, 2007, 12:27pm UTC](https://www.ruby-forum.com/t/redirect-foo-com-to-www-foo-com/115744/2 "2007-09-28T12:27:41Z")

</div>

Vince W. wrote:

> I tried adding :domain =\> ‘.foo.com’ but that didn’t seem to work. I  
> figure the nginx redirect would be the easiest thing…
> 
> Thanks.

I’m using something like the following and it seems to work OK:

```
server {
    server_name www.foo.com;
    rewrite ^/(.*) http://foo.com/$1 permanent;
}

```

## –

Autopendium :: Stuff about old cars  
[http://autopendium.com](http://autopendium.com)

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [September 28, 2007, 4:07pm UTC](https://www.ruby-forum.com/t/redirect-foo-com-to-www-foo-com/115744/3 "2007-09-28T16:07:59Z")

</div>

Yeah for some reason that doesn’t work for me. After I do that and go  
to [foo.com](http://foo.com) it instead redirects me to one of the other vhosts on that  
server. Weird.

On 9/28/07, Chris T [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > self.current\_user.remember\_token\_expires\_at }  
> > server\_name [www.foo.com](http://www.foo.com);

–  
support independent business – [http://www.buyindie.net/](http://www.buyindie.net/)

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [September 28, 2007, 4:26pm UTC](https://www.ruby-forum.com/t/redirect-foo-com-to-www-foo-com/115744/4 "2007-09-28T16:26:19Z")

</div>

Vince W. wrote:

> Yeah for some reason that doesn’t work for me. After I do that and go  
> to [foo.com](http://foo.com) it instead redirects me to one of the other vhosts on that  
> server. Weird.  
> Did you try with

RewriteEngine On

?

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [September 30, 2007, 12:04am UTC](https://www.ruby-forum.com/t/redirect-foo-com-to-www-foo-com/115744/5 "2007-09-30T00:04:25Z")

</div>

Ok, the “for some reason” I mentioned below turns out to be the order  
in which my vhosts were listed. The redirect always goes to the first  
vhost that was setup… probably a bug in nginx or perhaps a snafu in  
my config. Moving my [foo.com](http://foo.com) entry to the front of the line enabled  
your rewrite line to work.

Thanks,  
Vince

On 9/28/07, Vince W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > My current cookie is stored with this line:  
> > > Thanks.
> > 
> > * * *

–  
support independent business – [http://www.buyindie.net/](http://www.buyindie.net/)

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [October 1, 2007, 12:37am UTC](https://www.ruby-forum.com/t/redirect-foo-com-to-www-foo-com/115744/6 "2007-10-01T00:37:59Z")

</div>

On 29 Sep 2007, at 23:03, Vince W. wrote:

> Ok, the “for some reason” I mentioned below turns out to be the order  
> in which my vhosts were listed. The redirect always goes to the first  
> vhost that was setup… probably a bug in nginx or perhaps a snafu in  
> my config. Moving my [foo.com](http://foo.com) entry to the front of the line enabled  
> your rewrite line to work.

nginx defaults to the first vhost when it can’t find the host so it  
may well be your re-write isn’t working.

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [September 28, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/redirect-foo-com-to-www-foo-com/115744/7 "2007-09-28T16:35:31Z")

</div>

I hadn’t tried that, but I get this regardless of where I insert it:

Starting nginx: 2007/09/28 10:12:22 [emerg] 5696#0: unknown directive  
“RewriteEngine” in /etc/nginx/nginx.conf:21

I tried putting it both before sendfile on; and also inside my server  
block but with the same results.

-Vince

On 9/28/07, Joachim G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
support independent business – [http://www.buyindie.net/](http://www.buyindie.net/)

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [October 12, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/redirect-foo-com-to-www-foo-com/115744/8 "2007-10-12T19:57:44Z")

</div>

> ```
> server {
> server_name www.foo.com;
> rewrite ^/(.*) http://foo.com/$1 permanent;
> }
> 
> ```

This will be working only if host end with /, and you redirect from  
[www.foo.com](http://www.foo.com) to [foo.com](http://foo.com).

Try this example:

server {  
server\_name [foo.com](http://foo.com);  
rewrite ^(.\*)$ [http://www.foo.com$1](http://www.foo.com$1) permanent;  
}
