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 to To perform this setup on Nginx webserver, follow below steps:

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


  • 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
wp options update home

Leave a Reply

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