---
description: Route requests to a different origin, prepend a directory to the URL path, and remove specific segments.
title: Change origin and modify paths
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.

# Change origin and modify paths

Reroute a request to a different origin and modify the URL path.

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/route-and-rewrite/index.md)|[Agent setup](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/agent-setup/)

This example demonstrates how to use Cloudflare Snippets to:

* Reroute incoming requests to a different origin.
* Prepend a directory to the URL path.
* Remove specific segments from the URL path.

```js
export default {
	async fetch(request) {
		// Clone the original request to create a new request object
		const newRequest = new Request(request);

		// Add a header to identify a rerouted request at the new origin
		newRequest.headers.set("X-Rerouted", "1");

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

		// Step 1: Reroute to a different origin
		url.hostname = "example.com"; // Change the hostname to the new origin

		// Step 2: Append a directory to the path
		url.pathname = `/new-path${url.pathname}`; // Prepend "/new-path" to the current path

		// Step 3: Remove a specific segment from the path
		url.pathname = url.pathname.replace("/remove-me", ""); // Rewrite `/remove-me/something` to `/something`

		// Fetch the modified request from the updated URL
		return await fetch(url, newRequest);
	},
};
```

This configuration will perform the following rewrites:

| Request URL                       | URL after rewrite                |
| --------------------------------- | -------------------------------- |
| https://subdomain.example.com/foo | https://example.com/new-path/foo |
| https://example.com/remove-me/bar | https://example.com/new-path/bar |
| https://example.net/remove-me     | https://example.com/new-path     |

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/route-and-rewrite/#page","headline":"Change origin and modify paths · Cloudflare Rules docs","description":"Route requests to a different origin, prepend a directory to the URL path, and remove specific segments.","url":"https://developers.cloudflare.com/rules/snippets/examples/route-and-rewrite/","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":["URL rewrite"]}
```
