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

By Sushant Chawla

Over the past 11 years I have acquired very good knowledge of:

- Cloud Setup on Amazon, Rackspace with autoscaling, load balancers, etc
- LAMP architecture
- LNMP Architecture
- Apache, Nginx, Lighttpd
- Databases: MySQL and PostgreSQL
- Bash Scripting
- Configuration Management with Puppet
- Monitoring: Nagios, Zabbix, ICINGA
- Mail servers: Sendmail, Postfix, Exim
- Web Accelerators & Caching Setups with Memcache, Varnish & Redis
- Setup and Management of high availability applications clusters
- LDAP, Samba, NFS, FTP, DNS, DHCP, Openfire, Squid Proxy
- Subversion(SVN), GIT
- Control Panels: Plesk, Webmin, cPanel

Apart from Linux, I am a great sports star & regular traveller. I like working out and exploring various places.

Cheers ;-)

Leave a Reply

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