Nginx rules to redirect non-WWW to WWW URL?

Discussion in 'Nginx' started by poly1, Jun 4, 2011.

  1. #1
    I have a vbulletin forum registered with the following domain:
    http://studioleaks.info
    Code (markup):
    However the actual forum itself is located in a folder called "forums", under the "public" (nginx site root) directory. The only content that's in my site root is an under construction page, which I don't want visitors to see, so I need to make sure that all traffic redirects to:
    www.studioleaks.info/forums/
    Code (markup):
    Until recently I was using Apache/Cpanel, with the following rule placed in an .htaccess file in my forum root:
    RewriteCond %{HTTP_HOST} !^www\.studioleaks\.info
    RewriteRule (.*) http://www.studioleaks.info/forums/$1 [R=301,L]
    Code (markup):
    This worked to make sure that, regardless of whether a visitor typed in the www or non-www version of the site root, studioleaks.info (i.e. not the forum root), that they would be redirected to the www version of the forums:
    http://www.studioleaks.info/forums/
    Code (markup):
    I've switched to Nginx now. I have the following in my conf (some info removed for security/privacy reasons:
    server {
    		listen   80;
    		server_name site.studioleaks.info;
    		root   html;
    
    		location / {            
                }
    
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
            root   html;
                }
    }
    
    server {
    		listen 				server-ip-address:80;
    		server_name 		studioleaks.info;
    		rewrite ^ http://www.studioleaks.info/forums$request_uri? permanent;
    }
    
    server {
    		listen 		server-ip-address:80;
    		server_name www.studioleaks.info;
    		access_log 	/.../.../.../.../log/access.log;
    		error_log 	/.../.../.../.../log/error.log;
    		root 		/.../.../.../.../public;
    		index 		index.html index.php;
    Code (markup):
    Note:site.studioleaks.info is my FQDN. I used this instead of localhost (using an FQDN here and as my hostname in /etc/hosts is preferable to a non-FQDN domain, or am I mistaken?

    The end result is, if I type in the non-www version of 'studioleaks.info' (by itself, no /forums/), it forwards to the www version of:
    www.studioleaks.info/forums/[code]
    However if I type in the www version of 'studioleaks.info' (again, with no /forums/), it still loads the under construction page located at
    [code]www.studioleaks.info
    Code (markup):
    This is a big, big problem, since the www version of studioleaks.info is my preferred URL with Google, the listing that contains my sitelinks, etc.

    My question is: How do I make both the www and non-www versions of 'studioleaks.info' forward to:
    www.studioleaks.info/forums/
    Code (markup):
     
    poly1, Jun 4, 2011 IP
  2. poly1

    poly1 Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    grrrrrr.... this is really starting to get on my nerves. I've posted this on the Nginx forums and gotten zero responses. Someone MUST know how to redirect both a www and non-www version of the same domain to the same page!

    server {
    		listen			184.107.161.202:80;
    		server_name		studioleaks.info;
    		rewrite ^		http://www.studioleaks.info/forums$request_uri? permanent;
    }
    
    server {
    		listen			184.107.161.202:80 default_server;
    		server_name		www.studioleaks.info;
    		access_log		off;
    		error_log		/log/error.log;
    		root			/studioleaks.info/public;
    		index			index.php index.html;
    Code (markup):
    I've seen examples posted with both the www and non-www versions listed in a single block, like this:

    server {
            listen   80;
            server_name  example.com www.example.com;
            rewrite ^/(.*) http://www.example.com/$1 permanent;
    }
    Code (markup):
    But when I try that it results in a redirect loop. In fact any change I make seems to result in a redirect loop. How do I fix this? How hard is it to make www and non-www both point to /forums/ under nginx?
     
    poly1, Jun 7, 2011 IP
  3. stardust.x7

    stardust.x7 Active Member

    Messages:
    369
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    90
    #3
    Contact with ngnixcp, they will assist you regarding this issue. Free or, paid (less than 5$ hopefully).
     
    stardust.x7, Jun 8, 2011 IP