Middleware - Route Blocking Example

This middleware runs before every request and checks the incoming URL.
If the user tries to access the "/blocked" route, the middleware intercepts the request and immediately redirects them back to the homepage ("/").

How it works:
Next.js inspects the request pathname
If pathname === "/blocked", the middleware returns a redirect response
The user never reaches the original page

This pattern is useful for:

  • Protecting restricted routes
  • Redirecting users based on auth or roles
  • Blocking deprecated or disabled pages
  • Handling simple access rules at the edge

Click this link which attempts to access the blocked route: "/blocked". It will be redirected to Homepage.