---
title: Filtering query parameters
description: Learn how to filter query parameters in your Middleware.
url: "https://vercel.com/kb/guide/filter-query-parameters"
published: 2025-11-03
last_updated: 2025-11-11
authors: DX Team
install_vercel_plugin: npx plugins add vercel/vercel-plugin
---

The following example shows how to filter query parameters in your Middleware.

Your `middleware` file should be placed at the root of your project. If you are using the `src` directory, the file should be placed in the `src` directory.

```js
import { NextResponse } from 'next/server';

const allowedParams = ['allowed'];

export const config = {
  matcher: '/',
};

export default function middleware(request) {
  const url = request.nextUrl;
  let changed = false;

  url.searchParams.forEach((_, key) => {
    if (!allowedParams.includes(key)) {
      url.searchParams.delete(key);
      changed = true;
    }
  });

  // Avoid infinite loop by only redirecting if the query
  // params were changed
  if (changed) {
    return NextResponse.rewrite(url);
  }
}
```
```ts
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';

const allowedParams = ['allowed'];

export const config = {
  matcher: '/',
};

export default function middleware(request: NextRequest) {
  const url = request.nextUrl;
  let changed = false;

  url.searchParams.forEach((_, key) => {
    if (!allowedParams.includes(key)) {
      url.searchParams.delete(key);
      changed = true;
    }
  });

  // Avoid infinite loop by only redirecting if the query
  // params were changed
  if (changed) {
    return NextResponse.rewrite(url);
  }
}
```

## More resources

- [Middleware](/docs/routing-middleware)
  
- [Middleware API](/docs/routing-middleware/api)

## Related Documentation

- [Routing Middleware](/docs/routing-middleware)
- [Next.js](/docs/frameworks/full-stack/nextjs)

## More Routing Middleware guides

- [How to Optimize Next.js + Sitecore JSS](/kb/guide/how-to-optimize-next.js-sitecore-jss): This guide covers performance and usage considerations when building and deploying your Next.js and Sitecore JSS application.
- [Dynamic redirects with Global Config and Next.js proxy](/kb/guide/dynamic-redirects-with-global-config-and-next-js-proxy): Learn how to create redirects that update instantly without redeploying by storing rules in Global Config and reading them from your Next.js proxy.
- [Migrate to Vercel from Cloudflare](/kb/guide/migrate-to-vercel-from-cloudflare): Migrate your website's configuration from Cloudflare Pages or Workers to Vercel

## Reference Architecture

Choose a stack and work through the decisions behind it, with curated documentation, guides, templates, and courses to help you build.

- [Agent stack](/kb/architecture/agents): Build and operate agents with models, tools, and durable workflows.
- [Web applications](/kb/architecture/web): Choose rendering, data, authentication, and caching approaches.
- [Backends](/kb/architecture/backends): Choose a runtime, connect services, and run background work.

[Browse all stacks](/kb/architecture)

## Build with prompts

Build an app, extend an agent, or improve your project with ready-to-use prompts for your coding agent.

[Browse the prompt library](/kb/prompts) · [Read the Markdown index](/kb/prompts.md)
