#1  
Old 2nd July 2013, 15:52
jaypabs jaypabs is offline
Member
 
Join Date: Jun 2013
Posts: 69
Thanks: 0
Thanked 1 Time in 1 Post
Default 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;
	}
Reply With Quote
Sponsored Links
  #2  
Old 9th July 2013, 15:00
jaypabs jaypabs is offline
Member
 
Join Date: Jun 2013
Posts: 69
Thanks: 0
Thanked 1 Time in 1 Post
Default

No one knows about this?
Reply With Quote
  #3  
Old 9th July 2013, 15:35
falko falko is offline
Super Moderator
 
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 41,701
Thanks: 1,900
Thanked 2,749 Times in 2,579 Posts
Default

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;
}
__________________
Falko
--
Download the ISPConfig 3 Manual! | Check out the ISPConfig 3 Billing Module!

FB: http://www.facebook.com/howtoforge

nginx-Webhosting: Timme Hosting | Follow me on:
Reply With Quote
  #4  
Old 9th July 2013, 15:58
jaypabs jaypabs is offline
Member
 
Join Date: Jun 2013
Posts: 69
Thanks: 0
Thanked 1 Time in 1 Post
 
Default

Quote:
Originally Posted by falko View Post
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.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 14:44.


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