# Rewriting request to www.domain.com to domain.com

**URL:** <https://www.ruby-forum.com/t/rewriting-request-to-www-domain-com-to-domain-com/202502>\
**Category:** NGINX\
**Created:** [January 31, 2011, 12:00pm UTC](https://www.ruby-forum.com/t/rewriting-request-to-www-domain-com-to-domain-com/202502 "2011-01-31T12:00:00Z")\
**Posts on this page:** 5\
**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:** [January 31, 2011, 12:00pm UTC](https://www.ruby-forum.com/t/rewriting-request-to-www-domain-com-to-domain-com/202502/1 "2011-01-31T12:00:00Z")

</div>

Hello,

I am having a hard time getting the following type of domain rewrite to  
work. This is what I have so far:

```
 server {
     listen 80;

     charset off;
     server_name www.foobar.com;

     access_log /var/log/nginx/iknow_access.log cerelog;
     location / {
        rewrite ^ http://foobar.com permanent;
     }

     location /widgets/ {
        return 410;
     }
     location /banners/ {
        return 410;
     }

     error_page 404 410 /404.html;
     location = /404.html {
        root /data/foobar/current/public;
     }
 }

```

I want it to rewrite all requests to the [www.foobar.com](http://www.foobar.com) to the flat  
level domain of [foobar.com](http://foobar.com).

I.e. [www.foobar.com/awesomelink](http://www.foobar.com/awesomelink) should be [foobar.com](http://foobar.com)

But, I am getting [foobar.com/awesomelink](http://foobar.com/awesomelink) instead.

Also, the 410 responses are redirecting just like the location /  
This is with Nginx 0.7.65

Any ideas what I am doing wrong?

Thanks,  
Zev

---

<div class="post-metadata">

**Author:** ![Zev\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb8c5e/32.png) [@Zev\_B](https://www.ruby-forum.com/u/Zev_B)\
**Post date:** [January 31, 2011, 2:18pm UTC](https://www.ruby-forum.com/t/rewriting-request-to-www-domain-com-to-domain-com/202502/2 "2011-01-31T14:18:49Z")

</div>

On 31 Jan 2011 10h53 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

Your rewrite doesn’t work because you’re not passing along the request  
URI available in the $request\_uri variable.

Use 2 server blocks:

server {  
## This is to avoid the spurious if for sub-domain name  
## rewriting. See [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/Pitfalls#Server_Name).  
listen 80;

```
server_name www.foobar.com;
rewrite ^ $scheme://foobar.com$request_uri? permanent;

```

} # server domain rewrite.

```
 server {
     listen 80;

     charset off;
     server_name www.foobar.com;

     access_log /var/log/nginx/iknow_access.log cerelog;

     location / {

       (...)
     }

     location /widgets/ {
        return 410;
     }

     location /banners/ {
        return 410;
     }

     error_page 404 410 /404.html;

     location = /404.html {
        root /data/foobar/current/public;
     }
 }

```

> Any ideas what I am doing wrong?  
> Cf. above.

— appa

---

<div class="post-metadata">

**Author:** ![Zev\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb8c5e/32.png) [@Zev\_B](https://www.ruby-forum.com/u/Zev_B)\
**Post date:** [January 31, 2011, 2:50pm UTC](https://www.ruby-forum.com/t/rewriting-request-to-www-domain-com-to-domain-com/202502/3 "2011-01-31T14:50:54Z")

</div>

On Mon, Jan 31, 2011 at 07:53:58PM +0900, Zev B. wrote:

> ```
> charset off;
> location /banners/ {
> 
> ```
> 
> I want it to rewrite all requests to the [www.foobar.com](http://www.foobar.com) to the flat  
> level domain of [foobar.com](http://foobar.com).
> 
> I.e. [www.foobar.com/awesomelink](http://www.foobar.com/awesomelink) should be [foobar.com](http://foobar.com)
> 
> But, I am getting [foobar.com/awesomelink](http://foobar.com/awesomelink) instead.
> 
> Also, the 410 responses are redirecting just like the location /  
> This is with Nginx 0.7.65
> 
> Any ideas what I am doing wrong?

This configuration should work. Probably, your browser has cached  
old permanent redirects. Or your real configuration is more complex  
than this.

–  
Igor S.

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

---

<div class="post-metadata">

**Author:** ![Zev\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb8c5e/32.png) [@Zev\_B](https://www.ruby-forum.com/u/Zev_B)\
**Post date:** [January 31, 2011, 3:08pm UTC](https://www.ruby-forum.com/t/rewriting-request-to-www-domain-com-to-domain-com/202502/4 "2011-01-31T15:08:30Z")

</div>

On 31 Jan 2011 13h11 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 31 Jan 2011 10h53 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> Your rewrite doesn’t work because you’re not passing along the  
> request URI available in the $request\_uri variable.
> 
> Use 2 server blocks:

server {

## This is to avoid the spurious if for sub-domain name

## rewriting. See [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/Pitfalls#Server_Name).

listen 80;

server\_name [www.foobar.com](http://www.foobar.com);  
rewrite ^ $scheme://foobar.com permanent;  
} # server domain rewrite.

I’ve read incorrectly what you want to do. You just want to  
redirect all [www.foobar.com](http://www.foobar.com) requests to the root [foobar.com](http://foobar.com). Then the  
above works.

— appa

---

<div class="post-metadata">

**Author:** ![Zev\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb8c5e/32.png) [@Zev\_B](https://www.ruby-forum.com/u/Zev_B)\
**Post date:** [February 1, 2011, 3:42am UTC](https://www.ruby-forum.com/t/rewriting-request-to-www-domain-com-to-domain-com/202502/5 "2011-02-01T03:42:37Z")

</div>

Hello Igor and Antnio,

On 01/31/2011 10:49 PM, Igor S. wrote:

> This configuration should work. Probably, your browser has cached  
> old permanent redirects. Or your real configuration is more complex  
> than this.

Indeed, it was an error in another part of my configuration.  
Thanks for the confirmations!

Zev
