Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect a URL in Nginx

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;
            }
    }
like image 516
Amal Kumar S Avatar asked Apr 24 '12 08:04

Amal Kumar S


People also ask

How does NGINX redirection work?

Temporary and Permanent Nginx Redirect Explained On the other hand, a permanent Nginx redirect informs the web browser that it should permanently link the old page or domain to a new location or domain. To map this change, the redirects response code 301 is used for designating the permanent movement of a page.

How do I redirect http to https in NGINX?

Redirect HTTP to HTTPS version for Specified domain in Nginx Server_name domain-name.com www.domain-name.com – it specifies the domain names. So, replace it with your website domain name that you want to redirect. Return 301 https://domain-name.com$request_uri – it moves the traffic to the HTTPS version of the site.

How do I redirect a URL in Linux?

Use 301 for permanent redirect, 302 for temporary redirect. destination-url – relative or absolute URL path where you want the source URL to be redirected to.

What is return 301 in NGINX?

301 Permanent Redirection Permanent redirection will inform the browser that the website is no longer available with the old URL and this information should be updated and this point to the new URL we published.


4 Answers

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.

like image 186
Dmitry Verhoturov Avatar answered Oct 04 '22 15:10

Dmitry Verhoturov


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.

like image 45
Damien Justin Šutevski Avatar answered Oct 04 '22 15:10

Damien Justin Šutevski


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;
            }
    }
like image 42
Amal Kumar S Avatar answered Oct 04 '22 14:10

Amal Kumar S


This is the top hit on Google for "nginx redirect". If you got here just wanting to redirect a single location:

location = /content/unique-page-name {
  return 301 /new-name/unique-page-name;
}
like image 31
Seph Reed Avatar answered Oct 04 '22 13:10

Seph Reed