URL Redirect with a delay

Redirect with a delay using Meta tag in FastHTML in exception handler
FastHTML
Python
Web Development
Author

Isaac Flath

Published

August 19, 2024

Today I Learned

You can set up a delated redirect with the Meta HTML tag. Conveniently you can use this in a 404 exception handler if your site moved to catch any possible route/link users go to.

I did to redirect people to the new location of the FastHTML gallery site.

from fasthtml.common import *

new_url = "https://gallery.fastht.ml/"
def _not_found(req, exc):
    msg = Div(H1("We moved to ",A(href=new_url)(new_url))
            P("Please update your bookmark!  Redirecting in 3 seconds..."))
    return msg,Meta(http_equiv="refresh", content=f"3;url={new_url}")

app = FastHTML(exception_handlers={404:_not_found})

serve()