0

This question already has an answer here:

I have a server that will have 2 different types/formats of requests coming in. Both request URL's will start with: http://myredirect.mycompany.com/browse/. After '/browse/' it will either be a number:

http://myredirect.mycompany.com/browse/12345, it can be any number

or it will be 2 to 4 capital letters followed by a '-' followed by a number:

http://myredirect.mycompany.com/browse/QA-12345

What I want to do redirect to a different URL that's based on the incoming URL. For example:

http://myredirect.mycompany.com/browse/12345 would redirect to http://serverA.mycompany.com/show_item.cgi?12345

and

http://myredirect.mycompany.com/browse/QA-12345 would redirect to http://serverB.mycompany.com/browse/QA-12345

I think what I want is to use an nginx rewrite but I'm not sure how to form. From what I've googled, you can use regex in rewrites so I could use something like

^(/browse/[0-9])?

for the first one. But how do I actually pull that number into the redirect URL? Thanks!

marked as duplicate by Michael Lowman, mdpc, Sven Jan 6 '15 at 1:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

Reading nginx's documentation and the PCRE library documentation can help you understand how rewrite and regex work.

For your example you would need a location block wrapping two rewrite rules like this :

location /browse {
    rewrite ^/browse/(\d+)$ http://serverA.mycompany.com/show_item.cgi?$1 [flag];
    rewrite ^/browse/([A-Z]{2,4}-\d+)$ http://serverB.mycompany.com/browse/$1 [flag];
}

Replace [flag] with permanent or redirect depending on desired redirect code (301 or 302).

  • Perfect! Exactly what I was looking for. – fender4645 Jan 5 '15 at 22:44

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