HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials

HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials (http://www.howtoforge.com/forums/index.php)
-   Installation/Configuration (http://www.howtoforge.com/forums/forumdisplay.php?f=27)
-   -   Non-www to www and vice versa (http://www.howtoforge.com/forums/showthread.php?t=62426)

jaypabs 2nd July 2013 14:52

Non-www to www and vice versa
 
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;
        }


jaypabs 9th July 2013 14:00

No one knows about this?

falko 9th July 2013 14:35

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;
}


jaypabs 9th July 2013 14:58

Quote:

Originally Posted by falko (Post 299576)
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;
}


Cheers!

Thank you very much.


All times are GMT +2. The time now is 23:15.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.