Wednesday, January 30, 2013

Rewrite no-www to www with nginx

I had to redirect all users who entered the address without "www" to the address with "www".
For example:

  •  "" -> ""
  •  "" -> ""

I'm using Engine Yard and the nginx configuration files are there :

Here you can see DOMAIN.conf. Do not modify this file ! I did it and later I found out that my modification was removed. This happened when you "apply" or "upgrade" your environment, this file is re-created.
The file you should modify is /data/nginx/servers/DOMAIN/custom.conf
(If you look at the bottom of DOMAIN.conf, you can see that custom.conf is included)
Here is the code:

if ($host = {
    rewrite ^(.*)$1;

Then I was doing "touch tmp/restart.txt" and the redirection was not working...
Because in fact you should do this:
sudo /etc/init.d/nginx reload

Of course if you're using "https", you should modify "custom.ssl.conf" !

No comments: