Nginx redirect www.example.com to example.com

Jun 29, 2011   #nginx  #redirect 

Just switched to using Nginx instead of Apache for my personal hosting. I’m surprised by how much quicker it responds and how much nicer the syntax is. I’ll probably log a few more of these nuggets for future reference.

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

server {
    server_name  www.example.com;
    #Put here your domain hosting configuration.
}