Non-www to www and vice versa

Discussion in 'Installation/Configuration' started by jaypabs, Jul 2, 2013.

  1. jaypabs

    jaypabs New Member

    How can I rewrite nginx to redirect non-www to www and vice versa?

    I have tried the following by adding it in the nginx directives without luck.

    non-www to www:
    Code:
    	server {
    		server_name example.com;
    		rewrite ^(.*) http://www.example.com$1 permanent;
    	}
    
    www to non-www:
    Code:
    	server {
    		server_name www.example.com;
    		rewrite ^(.*) http://example.com$1 permanent;
    	}
    
     
  2. jaypabs

    jaypabs New Member

    No one knows about this?
     
  3. falko

    falko Super Moderator Howtoforge Staff Moderator HowtoForge Supporter ISPConfig Developer

    www to non-www:

    Code:
    if ($http_host != "example.com") {
      rewrite ^ $scheme://example.com$request_uri? permanent;
    }
    non-www to www:

    Code:
    if ($http_host != "www.example.com") {
      rewrite ^ $scheme://www.example.com$request_uri? permanent;
    }
     
  4. jaypabs

    jaypabs New Member

    Cheers!

    Thank you very much.
     

Share This Page