How to Do Page Redirections with Nginx.

301 vs 302 Redirection:

301 permanent redirection:

Search Engine: When a search engine gets 301 status code in the response header of a webpage, it understands that this webpage no longer exists so it takes the new URL and replace the indexed URL with the new one and also modifies pagerank

So search engine refreshes all indexed URL that no longer exist (301 found) with the new URL, this will retain your old webpage traffic, pagerank and divert it to the new one (you will not lose you traffic of old webpage).

Browser: if a browser finds 301 status code then it caches the mapping of the old URL with the new URL, the client/browser will not attempt to request the original location but use the new location from now on unless the cache is cleared.

302 temporary redirection:

Search Engine: Here there will not be any modifications of page rank for old site as 302 specified that the page is redirected temporarily.

Browser: The browser will not change cache mapping of old url with new url as its temporary redirection.

How to to redirection in Nginx:

Its very simple to implement redirection in nginx, only thin you have to do is change your project's nginx configuration located at /etc/nginx/site-available/ directory as shown below:

Redirect Non-www to www Redirect:

Permanent(301) redirection

server {
    listen 80;
    server_name domain1.com;
    return 301 $scheme://www.domain1.com$request_uri;
}

Temporary(302) redirection:

server {
    listen 80;
    server_name domain1.com;
    return 302 $scheme://www.domain1.com$request_uri;
}

Redirect www to non-www Redirect:

Permanent(301) redirection

server {
    listen 80;
    server_name www.domain1.com;
    return 301 $scheme://domain1.com$request_uri;
}

Temporary(302) redirection:

server {
    listen 80;
    server_name www.domain1.com;
    return 302 $scheme://domain1.com$request_uri;
}

Now check the configuration for errors, run

nginx -t

if no errors, restart nginx:

service nginx restart

So, its really that easy to redirect using nginx configuration

Posted On 16 May 2014 By MicroPyramid


Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Deploying Wordpress blog with Django using Docker Containers.

Containers run services in an isolated environment, if we deploy our applications using containers then the host system on which these containers are running will ...

Continue Reading...
Configuring WordPress Blog as sub-directory alongside Django in Nginx

Using regular expressions within Nginx we can bind urls to particular application servers, below we will configure wordpress blog and django site to be deployed ...

Continue Reading...
php7 hosting on Ubuntu Server with Nginx(LEMP stack)

PHP7 is the latest version with lot of performance improvements and every one want to update their servers and code to it. Lets see how ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties