nginx – how to nginx www redirect

nginx – how to nginx www redirect

After few days of study the nginx www redirect, I actually have one thing to put in writing regarding. It’s a awfully staple items however will say a decent begin too.

After few days of study concerning, I even have one factor to put in writing down concerning server. It’s a awfully basic things but can say an honest begin too.

I am penning this article for redirecting non WWW web site to WWW and WWW web site to non WWW exploitation server permanent redirection. In server similar to apache we tend to don’t have something like htaccess file which may be placed in webroot.

In server we’ve separate conf file for every domain we’ve designed which is editable by root users solely, this can be the most reason to not have server on shared server.

All the airt rules is written within the server tag in server configuration file. therefore Let’s have a glance regarding however we are able to place airt rule for the WWW.

Redirect www to non www

Here is the following code which we need to place in domain’s configuration file to make all URL non www.

server 
{
    server_name  www.onlinecode;
    rewrite ^(.*) http://onlinecode$1 permanent;
}

Redirect non www to www

Here is the code which we need to place in domain’s configuration file to make all URL www.

server 
{
    server_name  onlinecode;
    rewrite ^(.*) http://www.onlinecode$1 permanent;
}

One idea just came into mind while writing this article, We can redirect all request from one domain to another domain using server. Have a look at following code block for the same.

Redirect One url to Another url

server 
{
    server_name  onlinecode;
    rewrite ^(.*) http://new-onlinecode$1 permanent;
}

If we have any other better way to achieve the same the please do share here so other developers including me will come to know about the better practices for the server.

Leave a Comment

Your email address will not be published. Required fields are marked *

76  +    =  82

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US