Linux Harbour

Technology News of Linux & Open Source

Linux

Nginx website redirect

When we should redirect a website to a new web address (URL) in nginx, we may make use of Nginx rewrite insides the web site configuration block (ie. location { … }).

There are few different types of HTTP redirects, which are in different meaning to browsers. HTTP status code 301 & 302 are two common HTTP redirects.

HTTP status code: 301 Moved Permanently

Status code 301 is permanent redirects which tells browser to forget old URL and not attempt to access anymore. It is suitable for websites which moved to new address permanently.

Example 1: redirect whole web site (any addresses) to a single new web site address.

rewrite ^/ http://www.new_web_site.com permanent;

Example 2: redirect whole web site (any addresses) to new web site address with same path.

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

HTTP status code: 302 Found

Status code 302 is temporary redirects which tells browser a temporary redirection, it is suitable for system maintenance which requires to have temporary address changes.

Example: redirect whole web site (any addresses) to a single new web site address.

rewrite ^/ http://www.new_web_site.com redirect;

 

Sammy Fung

Sammy is the founder of the Linux Harbour.