Change origin and modify paths · Cloudflare Rules docs | Latest TMZ Celebrity News & Gossip | Watch TMZ Live Skip to content
Cloudflare Docs

Change origin and modify paths

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

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.
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 URLURL after rewrite
https://subdomain.example.com/foohttps://example.com/new-path/foo
https://example.com/remove-me/barhttps://example.com/new-path/bar
https://example.net/remove-mehttps://example.com/new-path

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.