wildcard path rewrite (with and without trailing slash)

Ic3m4n asked:

I have some old URLs I want to rewrite.
The URLs look like this:


Now I want all of the URLs above redirected to this URL: http://test.com/bbb/. How can I achieve this?

Right now, my redirects look like this:

rewrite ^/aaa/?$ https://$host/bbb/

But that does not catch all of my test cases above.

My answer:

If you’re redirecting a whole URL path and everything under it to a single URL, you don’t even need a relatively expensive rewrite and regexp; a location will do.

location /aaa {
    return 301 https://$host/bbb/;

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.