0

I'm trying to use nginx to redirect my main page (www.domain.com) to a subdirectory (www.domain.com/store). I have the redirect working, but whenever I use the domain name it will redirect me to the ip address (www.IP.com/store). This is my server nginx config. Thank you in advance for any help!

    server {
    listen       80 default_server;
    server_name *.domain.com;

    location  / {
        index  index.php index.html index.htm;
    }
    location = / {
        rewrite ^/store permanent;
    }

    root   /usr/local/www/nginx;

}

Lethjakman
  • 997
  • 9
  • 15

2 Answers2

0

You missed a space. rewrite ^/store permanent; will try to match '/store' at the beginning of the uri path (thanks to the ^) and if it matches, it will rewrite it to 'permanent'. Since this is inside location = /, it will never succeed. Instead, you need:

rewrite ^ /store permanent;
kolbyjack
  • 17,660
  • 5
  • 48
  • 35
  • Hey! Thank you for the response. I changed the config like you said, but it doesn't seem to be fixing the issue. – Lethjakman Jul 03 '12 at 16:41
  • Two things: first, that rewrite still needs to be inside location = /, and second, you're probably testing with a browser, and since you're using a permanent rewrite, the browser won't even check with the server anymore when you request domain.com/, it'll just use the cached rewrite. Try with curl, or if that's not an option, try another browser. – kolbyjack Jul 03 '12 at 17:28
  • I did keep the rewrite inside of the =/, and I am testing on a browser. I tried "curl -L domain.com", and it gave me a full return of my web page, but it doesn't say anything about what the title bar would be saying. Also I did clear the cache of my browser. – Lethjakman Jul 05 '12 at 16:26
  • Try curl -I domain.com and look for a Location header – kolbyjack Jul 05 '12 at 16:27
  • Looking at the headers you posted, the redirect to your ip address isn't coming from nginx, it's coming from PHP, since all of the redirects that send you to the ip address also have `X-Powered-By: PHP` headers – kolbyjack Jul 05 '12 at 18:49
  • I found the problem. The redirect that you gave me was right, the problem was actually some software that I was using. X-Cart was redirecting my webpage as soon as the PHP loaded. Now I changed the "http_host" and everything worked perfectly. Thank you so much for your help! – Lethjakman Jul 05 '12 at 22:01
0

Hm, I can't fit this in your post so I figured I should put it here. Is this good practice for stackoverflow? I tried "curl -l" and it just returned the "not found" that I get when it's not redirected. But I used "curl -liL domain.com" and this is what came out.

HTTP/1.1 301 Moved Permanently
Server: nginx/1.2.1
Date: Thu, 05 Jul 2012 17:30:01 GMT
Content-Type: text/html
Content-Length: 184
Location: http://domain.com/store
Connection: keep-alive

HTTP/1.1 301 Moved Permanently
Server: nginx/1.2.1
Date: Thu, 05 Jul 2012 17:30:01 GMT
Content-Type: text/html
Content-Length: 184
Location: http://domain.com/store/
Connection: keep-alive

HTTP/1.1 301 Moved Permanently
Server: nginx/1.2.1
Date: Thu, 05 Jul 2012 17:30:01 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.4.4
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Last-Modified: Thu, 05 Jul 2012 17:30:01 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
P3P: CP="NON CURa ADMa DEVa TAIa CONi OUR DELa BUS IND PHY ONL UNI PUR COM NAV DEM STA"
Set-Cookie: xid_1f463=9cc017ff6c74884850d5bdfba1bfd5ae; path=/store; domain=IP; httponly
Location: http://IP/store/?xid_1f463=9cc017ff6c74884850d5bdfba1bfd5ae

HTTP/1.1 302 Found
Server: nginx/1.2.1
Date: Thu, 05 Jul 2012 17:30:01 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.4.4
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Last-Modified: Thu, 05 Jul 2012 17:30:01 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
P3P: CP="NON CURa ADMa DEVa TAIa CONi OUR DELa BUS IND PHY ONL UNI PUR COM NAV DEM STA"
Set-Cookie: xid_1f463=9cc017ff6c74884850d5bdfba1bfd5ae; path=/store; domain=IP; httponly
Location: http://IP/store/?xid_1f463=9cc017ff6c74884850d5bdfba1bfd5ae

HTTP/1.1 301 Moved Permanently
Server: nginx/1.2.1
Date: Thu, 05 Jul 2012 17:30:01 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.4.4
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Last-Modified: Thu, 05 Jul 2012 17:30:01 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
P3P: CP="NON CURa ADMa DEVa TAIa CONi OUR DELa BUS IND PHY ONL UNI PUR COM NAV DEM STA"
Set-Cookie: xid_1f463=9cc017ff6c74884850d5bdfba1bfd5ae; path=/store; domain=IP; httponly
Location: /store/home.php

HTTP/1.1 200 OK
Server: nginx/1.2.1
Date: Thu, 05 Jul 2012 17:30:02 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.4.4
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Last-Modified: Thu, 05 Jul 2012 17:30:01 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
P3P: CP="NON CURa ADMa DEVa TAIa CONi OUR DELa BUS IND PHY ONL UNI PUR COM NAV DEM STA"
Set-Cookie: xid_1f463=e7d9abf146153d7a49e3f08bda47c008; path=/store; domain=IP; httponly
Set-Cookie: RefererCookie=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/store; domain=IP; httponly
Set-Cookie: store_language=en; expires=Fri, 05-Jul-2013 17:30:01 GMT; path=/store; domain=IP
Lethjakman
  • 997
  • 9
  • 15