Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I need to redirect following URL:

http://www.example.com/product/product-name/1234

to

http://www.example.com/details/product.jsp?pid=1234

Can somebody help me on this? We are using nginx 1.1.19. Also, we need a server side redirect (URL in client side's browser should stay as the first one), not an http forward.

Thanks.

============== add more information for debugging:

In the details/product.jsp file, I have something like this:

<link href="../docs/assets/css/bootstrap.css" rel="stylesheet">

And the bootstrap.css is under /usr/share/nginx/www/docs/assets/css/ . However, when I use rewrite rule:

rewrite ^/product/product-name/(\d+)$ /details/product.jsp?pid=$1;

The root directory somehow become /www/product, hence the details/product.jsp tries to find the bootstrap.css under:

/usr/share/nginx/www/product/docs/assets/css

which causes file not found error in the log.

Hope above information helps.

share|improve this question

1 Answer 1

If you'd done some basic searching you would have found the nginx rewrite module, which provides the rewrite directive.

rewrite ^/product/product-name/(\d+)$ /details/product.jsp?pid=$1;
share|improve this answer
    
Thanks for your answer. But this changed URL in client side browser. We want the URL to stay unchanged for users. Anyway I can do that? –  Yuchen Wang Feb 13 '13 at 23:40
    
You used the rule exactly as above? nginx should only redirect if the replacement is an absolute URL or the permanent or redirect flags are added. Are you sure that nginx is redirecting and not the product.jsp script? –  mgorven Feb 13 '13 at 23:43
    
Sorry I just tried again and actually got some error in nginx log: *1 open() "/usr/share/nginx/www/product/docs/assets/css/bootstrap.css" failed (2: No such file or directory), client: 192.168.2.3, server: localhost, request: "GET /product/docs/assets/css/bootstrap.css HTTP/1.1", host: "www.example.com", referrer: "http://www.taggto.com/product/product-name/1234" I noticed nginx got the root path wrong in this case: the css file is under directory /www, but it is looking for it under /www/product. –  Yuchen Wang Feb 14 '13 at 0:11
    
@YuchenWang Your HTML is referencing a non-existent file. –  mgorven Feb 14 '13 at 0:12
    
@ngorven, thanks for helping out. I just updated the question, hope it gives more information to debug. –  Yuchen Wang Feb 14 '13 at 0:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.