Welcome! Log In Create A New Profile

Advanced

Why Nginx redirect www to non-www urls?

Posted by mattiamerlini 
Why Nginx redirect www to non-www urls?
February 17, 2015 12:08PM
Hi!
As in the title my problem is about www e non www urls. I use nginxCP, a distribution of nginx that suites cPanel and WHM.
I need to be able to surf my magento sites hosted by this server with both non-www and www urls.
Ex: when I type "www.mysite.com" I surf this site with www, when I type "mysite.com" I surf this site with www. No redirect

Is there a way to do this?

In attachment you can see my config file for nginx and a site's vhost (that ise automatically generated by nginxCP)

Thank you in advance
Attachments:
open | download - nginx.txt (3.2 KB)
Re: Why Nginx redirect www to non-www urls?
February 17, 2015 12:49PM
server_name www.mydomain.eu mydomain.eu;

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Why Nginx redirect www to non-www urls?
February 18, 2015 03:16AM
itpp2012 Wrote:
-------------------------------------------------------
> server_name www.mydomain.eu mydomain.eu;


Thank you itpp2012, but I've already tried this solution and it still not working

Any other solutions?

I can remove server_name_in_redirect on; from nginx.conf but doesn't work



Edited 1 time(s). Last edit at 02/18/2015 03:18AM by mattiamerlini.
Re: Why Nginx redirect www to non-www urls?
February 18, 2015 04:01AM
Can you explain what you are trying to do?

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Why Nginx redirect www to non-www urls?
February 19, 2015 11:52AM
itpp2012 Wrote:
-------------------------------------------------------
> Can you explain what you are trying to do?


I'm trying to allow both navigation of a site with ww and without www.
In attachment I inserted a photo that explains my need

Thanks
Attachments:
open | download - allow-both-ww-and-nonwww.png (26.5 KB)
Re: Why Nginx redirect www to non-www urls?
February 19, 2015 12:13PM
server_name www.mysite.com mysite.com;

should work, I use it too and just tested it again and this works fine, are you sure nginx is restarting ok?

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Why Nginx redirect www to non-www urls?
February 19, 2015 12:20PM
itpp2012 Wrote:
-------------------------------------------------------
> server_name www.mysite.com mysite.com;
>
> should work, I use it too and just tested it again and this works
> fine, are you sure nginx is restarting ok?


Now I will test again. I have to remove server_name_in_redirect on; set it to off or set it to on?

Thanks a lot
Re: Why Nginx redirect www to non-www urls?
February 19, 2015 02:13PM
Comment it out, I've never seen it used.

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Why Nginx redirect www to non-www urls?
February 19, 2015 06:28PM
I commented out it but nothing works.
Nginx restarts with no problem
Have you found out something strange, unusual or wrong in my config files?
Re: Why Nginx redirect www to non-www urls?
February 20, 2015 05:52AM
Can't see anything wrong in your conf, enable debug mode and see what the logs say.

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Why Nginx redirect www to non-www urls?
February 20, 2015 05:20PM
I enabled the debug mode and in the log file I found out this line:
"^(GET|HEAD)$" matches "GET", client: xxx.xxx.xxx.xxx, server: www.mattiamerlini.it, request: "GET / HTTP/1.1", host: "mattiamerlini.it", referrer: "http://mattiamerlini.it/"

I think the referrer is the problem.

Is the problem clearer for you now?

Thanks
Re: Why Nginx redirect www to non-www urls?
February 20, 2015 06:06PM
See near the end of http://forum.nginx.org/read.php?11,256802

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Why Nginx redirect www to non-www urls?
February 20, 2015 06:33PM
I've read this topic and tried, but nothing has changed. I have made some test mistakes and nginx noticed them, so my configs are loaded by the web server.
I can't figured out what is the problem



Edited 1 time(s). Last edit at 02/20/2015 06:34PM by mattiamerlini.
Re: Why Nginx redirect www to non-www urls?
February 21, 2015 06:20AM
> Ex: when I type "www.mysite.com" I surf this site with www, when I type "mysite.com" I surf this site with www. No redirect

Can you explain what is not happening or what you expect to happen? both server names are in the server block and both should handle www.xxx and xxx

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Why Nginx redirect www to non-www urls?
February 21, 2015 11:36AM
I resolved it after a deep study of my sites. In my server I have two sites: the first is written by me (a managerial software), and the second uses WordPress. I noticed out that the first site can be surfed with www and without-www, so I deduced that the problem wasn't the web server, but the CMS. After some researches on the web about this thing, I found out that WordPress (and also Magento) forces all the urls to be non-www (or the contrary, depending on WP version).
I fixed this problem with WP by adding this few line of code at the end of wp-config.php file, in the site's root:

$hostname = $_SERVER['SERVER_NAME'];
define('WP_SITEURL', 'http://' . $hostname);
define('WP_HOME', 'http://' . $hostname);

In Magento I can fix this problem with these methods: https://docs.nexcess.net/article/changing-magento-base-urls.html

Ps: if you want to test the non-CMS site's urls are http://www.merlinimediazioni.com and http://merlinimediazioni.com ; the CMS site's urls are http://www.mattiamerlini.it and http://mattiamerlini.it . When you load one of these urls you can notice that if you have inserted www.mattiamerlini.it the url remain with www, and viceversa for non-www.

Thank you for your time and help

Have a nice weekend
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 143
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready