@@ -14,41 +14,41 @@ description: Route requests to a different origin, prepend a directory to the UR
1414
1515This example demonstrates how to use Cloudflare Snippets to:
1616
17- * Reroute incoming requests to a different origin.
18- * Prepend a directory to the URL path.
19- * Remove specific segments from the URL path.
17+ - Reroute incoming requests to a different origin.
18+ - Prepend a directory to the URL path.
19+ - Remove specific segments from the URL path.
2020
2121``` js
2222export default {
23- async fetch (request ) {
24- // Clone the original request to create a new request object
25- const newRequest = new Request (request);
23+ async fetch (request ) {
24+ // Clone the original request to create a new request object
25+ const newRequest = new Request (request);
2626
27- // Add a header to identify a re-routed request at the new origin
28- newRequest .headers .set (" X-Rerouted" , " 1" );
27+ // Add a header to identify a rerouted request at the new origin
28+ newRequest .headers .set (" X-Rerouted" , " 1" );
2929
30- // Clone and parse the original URL
31- const url = new URL (request .url );
30+ // Clone and parse the original URL
31+ const url = new URL (request .url );
3232
33- // Step 1: Reroute to a different origin
34- url .hostname = " example.com" ; // Change the hostname to the new origin
33+ // Step 1: Reroute to a different origin
34+ url .hostname = " example.com" ; // Change the hostname to the new origin
3535
36- // Step 2: Append a directory to the path
37- url .pathname = ` /new-path${ url .pathname } ` ; // Prepend "/new-path" to the current path
36+ // Step 2: Append a directory to the path
37+ url .pathname = ` /new-path${ url .pathname } ` ; // Prepend "/new-path" to the current path
3838
39- // Step 3: Remove a specific segment from the path
40- url .pathname = url .pathname .replace (" /remove-me" , " " ); // Rewrite `/remove-me/something` to `/something`
39+ // Step 3: Remove a specific segment from the path
40+ url .pathname = url .pathname .replace (" /remove-me" , " " ); // Rewrite `/remove-me/something` to `/something`
4141
42- // Fetch the modified request from the updated URL
43- return await fetch (url, newRequest);
44- }
42+ // Fetch the modified request from the updated URL
43+ return await fetch (url, newRequest);
44+ },
4545};
4646```
4747
4848This configuration will perform the following rewrites:
4949
50- | Request URL | URL after rewrite |
51- | ----------------------------------------- | ---------------------------------- |
52- | ` https://subdomain.example.com/foo ` | ` https://example.com/new-path/foo ` |
53- | ` https://example.com/remove-me/bar ` | ` https://example.com/new-path/bar ` |
54- | ` https://example.net/remove-me ` | ` https://example.com/new-path/ ` |
50+ | Request URL | URL after rewrite |
51+ | ----------------------------------- | ---------------------------------- |
52+ | ` https://subdomain.example.com/foo ` | ` https://example.com/new-path/foo ` |
53+ | ` https://example.com/remove-me/bar ` | ` https://example.com/new-path/bar ` |
54+ | ` https://example.net/remove-me ` | ` https://example.com/new-path ` |
0 commit comments