How to implement vanity URLs in a legacy Flask app?

127 views Asked by At

I'm facing a problem where I need to redirect or replace existing URLs in a legacy Flask app to a more "vanity" URL scheme.

For instance:

www.example.org/camp -> really points to https://example.org/connect/rally_camps/register

While I managed to make this work using nginx config (this is using the typical uwsgi + reverse proxy nginx config to be serverd):

location /camp {
       rewrite ^/.* https://example.org/connect/rally_camps/register permanent;
}

When I hit the vanity URL I'm redirected to the non-vanity URL (long one). This obviously looks ugly...I'm not sure if there is a way to tell nginx to redirect but keep the same URL or this is something that requires some Flask work...301 redirects when user hits vanity URL to long URL maybe? But I think this will change URL again...any ideas?

Thanks!

1

There are 1 answers

1
Dag Høidahl On BEST ANSWER

Assuming you don't need to capture whatever comes after /camp/, this nginx configuration should do it:

location /camp {
       rewrite ^/.* /connect/rally_camps/register ;
}

From the nginx docs for rewrite:

If a replacement string starts with “http://”, “https://”, or “$scheme”, the processing stops and the redirect is returned to a client.

In other words, if you don't want a redirect, the replacement string cannot start with any of these prefixes.

The permanent flag also yields a redirect, so you can't use that either.