In Next.js middleware, when a request arrives, the middleware function runs first. It checks the request path. If the path is '/old', it redirects the browser to '/new', changing the browser's URL. If the path is '/rewrite', it rewrites the request internally to '/api/data' without changing the browser URL. The middleware returns immediately after redirect or rewrite, so only one action happens per request. Redirect tells the browser to load a new page, while rewrite serves different content behind the scenes. This helps manage URLs and content dynamically before the page loads.