Logo

dev-resources.site

for different kinds of informations.

Fallback Routing with Axum

Published at
2/27/2024
Categories
axum
rust
webdev
routing
Author
coreyja
Categories
4 categories in total
axum
open
rust
open
webdev
open
routing
open
Author
7 person written this
coreyja
open
Fallback Routing with Axum

Yesterday I ran into a small problem with my Axum routing for my blog. So today I wanted to take 10 minutes and do quick write up about it!

My blog uses Axum as its Web Framework of choice. I was looking through some old emails and found one where a reader reached out about a post I made! (It was this one about FZF Spell Checking in VIM)

But I realized that it was linking to an older URL format, The link was to https://coreyja.com/blog/2018/11/10/vim-spelling-suggestions-fzf.html!

And you can see I was using a different URL structure then, and my current blog didn't have a redirect setup!

And that was our goal! I wanted https://coreyja.com/blog/2018/11/10/vim-spelling-suggestions-fzf.html to redirect to /posts/vim-spelling-suggestions-fzf. And I also decided that I wanted blog/anything-else-here to redirect to /posts

Simple enough!

I added these two routes to my Axum Router function:

.route("/blog/:year/:month/:date/:slug", get(redirect_to_new_post_url))
.route("/blog/*catchall", get(redirect_to_posts_index))
Enter fullscreen mode Exit fullscreen mode

And I expected this to work out of the box! But it didn't :sadnerd:

Instead I got an error about conflicting routes. Luckily the answer is relatively easy, at least in my case! I just needed to use nest to target all the /blog urls and then use a fallback. Here is what I ended up with!

.nest(
  "/blog",
  Router::new()
    .route("/:year/:month/:date/:slug", get(redirect_to_new_post_url))
    .fallback(redirect_to_posts_index)
)
Enter fullscreen mode Exit fullscreen mode

And now I was able to get all the redirects working as I wanted! Go check out that 'old' URL above now and with any luck you should be redirected to the right post!

routing Article's
30 articles in total
Favicon
Mastering Nested Navigation in Flutter with `go_router` and a Bottom Nav Bar
Favicon
Understanding ShellRoute in go_router: Managing Shared Layouts Effectively
Favicon
How the Web Evolved: The Rise of Single Page Applications
Favicon
Introducing Humiris MoAI Basic : A New Way to Build Hybrid AI Models
Favicon
TanStack Router: The Future of React Routing in 2025
Favicon
Next.js: Dynamic Routing with API Integration.
Favicon
Learning Next.js 13 App Router: A Comprehensive Guide 🚀
Favicon
Organizing Your Routes Modularly and Automatically in Lithe
Favicon
Organizando Suas Rotas de Forma Modular e Automática no Lithe
Favicon
🗺️ Peta Jalan Laravel: Menjelajah Routing, Middleware, dan Controller (Indonesian Version)
Favicon
Working On a React / Python Flask Back End Web App.
Favicon
Vue.js for Beginners 2024 #VueJs Part 5 : A Complete Guide to Routing with Vue Router
Favicon
Mastering Routing Protocols with Cisco Packet Tracer: A Learning Experience
Favicon
Restful Routing - A Flask API Example
Favicon
Routing in React vs. Next.js: Extra Work or Easy Wins?
Favicon
Browser refresh on click of Home button using href
Favicon
Decorate the Symfony router to add a trailing slash to all URLs
Favicon
Routing in Umbraco Part 2: Content Finders
Favicon
Routing in Umbraco Part 1: URL segments
Favicon
Simplifying Network Administration: BGP Route Servers' Function in the Internet Ecosystem
Favicon
createBrowserRouter A step up from Switch
Favicon
Mastering Angular 17 Routing
Favicon
Snag the Current URL in SvelteKit (2024 Edition)
Favicon
Ensuring Type Safety in Next.js Routing
Favicon
Laravel 11: Health Check Routing Example
Favicon
Switch'in L2 mi L3 mü Olduğunun Öğrenilmesi
Favicon
Routing with React Router
Favicon
Routing implementation using PHP attributes
Favicon
What is Vinxi, and how does it compare to Vike?
Favicon
Fallback Routing with Axum

Featured ones: