---
title: Filtering query parameters
description: Learn how to filter query parameters in your Middleware.
url: /kb/guide/filter-query-parameters
canonical_url: "https://vercel.com/kb/guide/filter-query-parameters"
published: 2025-11-03
last_updated: 2025-11-11
authors: DX Team
related:
  - /docs/routing-middleware
  - /docs/routing-middleware/api
install_vercel_plugin: npx plugins add vercel/vercel-plugin
---
<!-- docsgraph:related -->
## Related pages

> **For AI agents:** Follow these links to understand how this page connects to the rest of the Vercel ecosystem. For the full cross-link map (inbound, outbound, prerequisites, and semantic neighbors), see the .graph.md link below.

- [Getting Started](https://vercel.com/docs/routing-middleware/getting-started?from=related) — Learn how you can use Routing Middleware, code that executes before a request is processed on a site, to provide speed a
- [Modifying request headers](https://vercel.com/kb/guide/modify-request-headers?from=related) — Learn how to modify request headers in your Middleware.
- [Proxy and Routing](https://vercel.com/docs/platforms/multi-tenant-platforms/middleware-and-routing?from=related) — Resolve tenants and route requests by subdomain, custom domain, or path using Next.js Proxy on Vercel.
- [Backend for Frontend](https://nextjs.org/docs/app/guides/backend-for-frontend?from=related) — Learn how to use Next.js as a backend framework
- [Redirecting](https://nextjs.org/docs/pages/guides/redirecting?from=related) — Learn the different ways to handle redirects in Next.js.
- [Filtering](https://vercel.com/docs/analytics/filtering?from=related) — Learn how filters allow you to explore insights about your website's visitors.
- [useParams](https://nextjs.org/docs/pages/api-reference/functions/use-params?from=related) — API Reference for the useParams hook in the Pages Router.
- [Configuration Redirects](https://vercel.com/docs/routing/redirects/configuration-redirects?from=related) — Learn how to define static redirects in your framework configuration or vercel.json with support for wildcards, pattern
- [Proxy](https://nextjs.org/docs/pages/api-reference/file-conventions/proxy?from=related) — Learn how to use Proxy to run code before a request is completed.
- [Rendering content based on device](https://vercel.com/kb/guide/rendering-content-based-on-device?from=related) — Learn how to render different content based on the user agent in your Middleware.
- [Adding a response header](https://vercel.com/kb/guide/add-response-header?from=related) — Learn how to add a response header in your Middleware.
- [Using the crypto Web API to redirect requests with a unique token](https://vercel.com/kb/guide/use-crypto-web-api?from=related) — Learn how to use the Crypto Web API in your Middleware.

Full cross-link map for this page: [/kb/guide/filter-query-parameters.graph.md](/kb/guide/filter-query-parameters.graph.md)
<!-- /docsgraph:related -->


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)