I have a set of URLs I'm doing 301 redirects for (using Nginx, which I think uses PCRE for its Regex engine). This is a simplified version of what's required:
/old/- redirects to
/new/
- redirects to
/old/1234/- redirects to
/new/1234/
- redirects to
/old/1234/foo/- redirects to
/new/1234/foo/
- redirects to
/old/1234/bar/- redirects to
/new/1234/bar/
- redirects to
/old/1234/expired/- redirects to
/new/1234/
- redirects to
i.e. an over-simplification of this could be to say that /old/(.*) redirects to /new/$1, except for when the last part of the url is /[0-9]+/expired/, in which case it just goes up a level. (except I really need to keep it more specific with the foo, bar expired and 1234 parts matching the URL).
I want to cover all this in one regex if possible, rather than have multiple rules for each variation.
So my regex in the Nginx.conf so far is something like:
location ~* ^/old/(([0-9]+/)expired/)?|([0-9]+/(foo|bar/)?)?$ {
return 301 /new/$1;
}
Obviously that's not right. In summary:
- I want to get just the
/[0-9]+/part if the url ends with/expired/. - And the whole
/[0-9]+/foo/or/[0-9]+/bar/part if it ends in foo or bar. - And just the
/[0-9]+/part if that's what it ends with. - And if it just ends in
/old/then simply redirect to/new/