By Amal Kumar S


2012-04-24 08:39:54 8 Comments

I need to redirect every http://test.com request to http://www.test.com. How can this be done.

In the server block I tried adding

 rewrite ^/(.*) http://www.test.com/$1 permanent;

but in browser it says

  The page isn't redirecting properly

  Firefox has detected that the server is redirecting the request for 
  this address in a way that will never complete.

my server block looks like

 server {
            listen       80;
            server_name  test.com;
            client_max_body_size   10M;
            client_body_buffer_size   128k;

            root       /home/test/test/public;
            passenger_enabled on;
            rails_env production;

            #rewrite ^/(.*) http://www.test.com/$1 permanent;
            #rewrite ^(.*)$ $scheme://www.test.com$1;

            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                    root   html;
            }
    }

3 comments

@Dmitry Verhoturov 2012-04-24 18:29:54

Best way to do what you want is to add another server block:

server {
        #implemented by default, change if you need different ip or port
        #listen *:80 | *:8000;
        server_name test.com;
        return 301 $scheme://www.test.com$request_uri;
}

And edit your main server block server_name variable as following:

server_name  www.test.com;

Important: New server block is the right way to do this, if is evil. You must use locations and servers instead of if if it's possible. Rewrite is sometimes evil too, so replaced it with return.

@RCCola 2013-02-14 01:17:50

You could also end that server block with return 301 http://wwww.test.com$request_uri; instead of the rewrite.

@JCotton 2013-08-07 03:06:38

ditto @RCCola on using return instead of rewrite. See docs

@Jhn 2016-02-17 10:54:29

Question, how should the server blocks be ordered? the redirect then the main server block or main server block then the redirect? Because I have the same problem, stackoverflow.com/questions/35451929/…

@Dmitry Verhoturov 2016-02-19 18:16:08

Nginx config in mentioned question is correct, order does not matter.

@Nick Merrill 2016-02-21 17:35:20

Just a quick warning: Watch out for the 301 redirect vs the 302. You could be stuck with a cached permanent redirect, which makes it very troublesome to change your settings for clients that have already accessed a URL. (I know the OP asked for a 301, but be sure that's really what you want.)

@Damien Justin Ĺ utevski 2013-07-26 18:17:56

Similar to another answer here, but change the http in the rewrite to to $scheme like so:

server {
        listen 80;
        server_name test.com;
        rewrite     ^ $scheme://www.test.com$request_uri? permanent;
}

And edit your main server block server_name variable as following:

server_name  www.test.com;

I had to do this to redirect www.test.com to test.com.

@Amal Kumar S 2012-04-24 09:57:55

First make sure you have installed Nginx with the HTTP rewrite module. To install this we need to have pcre-library

How to install pcre library

If the above mentioned are done or if you already have them, then just add the below code in your nginx server block

  if ($host !~* ^www\.) {
    rewrite ^(.*)$ http://www.$host$1 permanent;
  }

To remove www from every request you can use

  if ($host = 'www.your_domain.com' ) {
   rewrite  ^/(.*)$  http://your_domain.com/$1  permanent;
  }

so your server block will look like

  server {
            listen       80;
            server_name  test.com;
            if ($host !~* ^www\.) {
                    rewrite ^(.*)$ http://www.$host$1 permanent;
            }
            client_max_body_size   10M;
            client_body_buffer_size   128k;

            root       /home/test/test/public;
            passenger_enabled on;
            rails_env production;

            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                    root   html;
            }
    }

Related Questions

Sponsored Content

7 Answered Questions

[SOLVED] How can I redirect and append both stdout and stderr to a file with Bash?

28 Answered Questions

[SOLVED] How to make a redirect in PHP?

  • 2009-04-20 14:13:22
  • Sam
  • 2404717 View
  • 1082 Score
  • 28 Answer
  • Tags:   php redirect

58 Answered Questions

[SOLVED] How do I redirect to another webpage?

2 Answered Questions

nginx change root folder for specyfic url

  • 2013-06-27 07:24:06
  • kabra
  • 11414 View
  • 6 Score
  • 2 Answer
  • Tags:   nginx path root

18 Answered Questions

[SOLVED] How to modify the URL without reloading the page?

31 Answered Questions

[SOLVED] How to manage a redirect request after a jQuery Ajax call

11 Answered Questions

[SOLVED] Node.js + Nginx - What now?

  • 2011-02-15 20:49:02
  • Van Coding
  • 318437 View
  • 898 Score
  • 11 Answer
  • Tags:   node.js nginx concept

6 Answered Questions

[SOLVED] How do I redirect with JavaScript?

  • 2011-01-20 08:08:18
  • Ms_Lucky13
  • 1303566 View
  • 874 Score
  • 6 Answer
  • Tags:   javascript redirect

1 Answered Questions

Wordpress constant redirect with nginx upstream

1 Answered Questions

[SOLVED] Nginx configuration resulting in too many connections

Sponsored Content