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

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 to the flat
level domain of foobar.com.

I.e. www.foobar.com/awesomelink should be foobar.com

But, I am getting 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

On 31 Jan 2011 10h53 WET, [email protected] 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 Pitfalls and Common Mistakes | NGINX.
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

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 to the flat
level domain of foobar.com.

I.e. www.foobar.com/awesomelink should be foobar.com

But, I am getting 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.
http://sysoev.ru/en/

On 31 Jan 2011 13h11 WET, [email protected] wrote:

On 31 Jan 2011 10h53 WET, [email protected] 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 Pitfalls and Common Mistakes | NGINX.

listen 80;

server_name 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 requests to the root foobar.com. Then the
above works.

— appa

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