---
description: Redirect requests to certain URLs based on a mapped object to the request's URL.
title: Bulk redirect based on a map object
image: https://developers.cloudflare.com/og-docs.png
---

[Skip to content](#main-content)

> Documentation Index  
> Fetch the complete documentation index at: https://developers.cloudflare.com/rules/llms.txt  
> Use this file to discover all available pages before exploring further.

# Bulk redirect based on a map object

Redirect requests to certain URLs based on a mapped object to the request's URL.

Last updated Aug 24, 2026|Copy as Markdown|[View as Markdown](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/rules/snippets/examples/bulk-redirect-map/index.md)|[Agent setup](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/agent-setup/)

```js
export default {
	async fetch(request) {
		// Define a variable with the hostname that needs to be redirected.
		const externalHostname = "example.com";

		// Define the map object. Replace the sources (/pathX) and targets (/redirectX) with ones that apply to your case.
		const redirectMap = new Map([
			["/path1", "https://" + externalHostname + "/redirect1"],
			["/path2", "https://" + externalHostname + "/redirect2"],
			["/path3", "https://" + externalHostname + "/redirect3"],
			["/path4", "https://cloudflare.com"],
		]);

		// Clone the original URL.
		const requestURL = new URL(request.url);

		// Check the request path against the map and redirect accordingly.
		const path = requestURL.pathname;
		const location = redirectMap.get(path);

		if (location) {
			return Response.redirect(location, 301);
		}

		// If request path not in map, return the original request.
		return fetch(request);
	},
};
```

Was this helpful?

YesNo

## On this page

[![](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/_astro/logo.te5VL_aD.svg)Docs](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/)

```json
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/rules/snippets/examples/bulk-redirect-map/#page","headline":"Bulk redirect based on a map object · Cloudflare Rules docs","description":"Redirect requests to certain URLs based on a mapped object to the request's URL.","url":"https://developers.cloudflare.com/rules/snippets/examples/bulk-redirect-map/","inLanguage":"en","image":"https://developers.cloudflare.com/og-docs.png","dateModified":"2026-08-24","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"},"keywords":["Redirects"]}
```
