Redirect from non www to www URL on Nginx

At times we come across requests from customers when they would like to redirect their non www URLs to www URLs. For example if a customer asks to force redirect his site from http://mylinuxtips.info to http://www.mylinuxtips.info To perform this setup on Nginx webserver, follow below steps:

  •  First goto Nginx vhost configuration of domain mylinuxtips.info & put this line in the configuration file of domain
if ($host !~ "^www." ) { rewrite ^/(.*)$ http://www.$host/$1 permanent; }

Selection_001

  • In case the website is based on WordPress CMS then you have to change the entries in WordPress database too. You can either use phpMyAdmin or you may use below commands to accomplish this change
wp options update siteurl www.mylinuxtips.info
wp options update home www.mylinuxtips.info

Leave a Reply

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

*