0

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!

1 Answers1

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).

Xavier Lucas
  • 13,095
  • 2
  • 44
  • 50