Edge Developer Platform
  • Edge Functions
    • Overview
    • Getting Started
    • Operation Guide
      • Function Management
      • Web Debugging
      • Function Trigger
      • Environment Variable
      • Code Replica
    • Runtime APIs
      • addEventListener
      • Cache
      • Cookies
      • Encoding
      • Fetch
      • FetchEvent
      • Headers
      • Request
      • Response
      • Streams
        • ReadableStream
        • ReadableStreamBYOBReader
        • ReadableStreamDefaultReader
        • TransformStream
        • WritableStream
        • WritableStreamDefaultWriter
      • Web Crypto
      • Web standards
      • Images
        • ImageProperties
    • Sample Functions
      • Example Overview
      • Obtaining Client URL Information
      • Customization Based on Client Geo Location
      • Obtaining Client Geo Location Information
      • Batch Redirect
      • URL rewrite based on regular expressions
      • Returning an HTML Page
      • Returning a JSON Object
      • Fetch Remote Resources
      • Authenticating a Request Header
      • Modifying a Response Header
      • Performing an A/B Test
      • Setting Cookies
      • Performing Redirect Based on the Request Location
      • Using the Cache API
      • Caching POST Requests
      • Responding in Streaming Mode
      • Merging Resources and Responding in Streaming Mode
      • Protecting Data from Tampering
      • Rewriting a m3u8 File and Configuring Authentication
      • Adaptive Image Resize
      • Image Adaptive WebP
      • Customize Referer restriction rules
      • Remote Authentication
      • HMAC Digital Signature
      • Naming a Downloaded File
      • Obtaining Client IP Address
      • Complex origin-pull URL rewriting
      • Web Bot Auth
    • Practical Tutorial
      • Overview
      • Origin retrieval based on user IP/geographic location
        • EdgeOne Implementation of Session Persistence Based on Client IP Addresses
        • EdgeOne Implementation of Origin-Pull Based on Client's Geo Location
      • APK dynamic packaging
        • EdgeOne enables dynamic packaging of Android APKs.
          • Feature Overview
          • Step 1: Preprocess the Android APK Parent Package
          • Step 2: Write the Channel Information into the APK Package with EdgeOne Edge Functions
      • Canary Release and Region-specific Execution
      • Adaptive Image Format Conversion via Edge Functions
      • Two Ways to Implement CDN Origin-pull Via Edge Function: Fetch and Passthrough
  • KV Storage
    • Overview
    • Operation Guide
  • Edge reasoning
    • Edge Inference Overview
    • Quick Guide

Complex origin-pull URL rewriting

In some business scenarios, you need to modify client request URLs before sending them to the origin server. This example implements two complex URL rewriting scenarios using Edge functions:
1. Path regex replacement: Using regex capture groups for path replacement, replacing paths starting with /a with /path-a while preserving the subsequent path structure. Path regex replacement enables seamless mapping from old paths to new paths, for example when resource directory structures change during content management system updates. Regex capture groups preserve key parts of the original path to ensure content remains accessible while adapting to the new directory structure.
2. Path case conversion: Converting the entire path to lowercase, conforming to Web standards best practices. For websites using object storage services (such as Tencent Cloud Object Storage) as their origin, since object storage typically distinguishes between uppercase and lowercase, uniform conversion can simplify resource management and prevent resource access failures due to case sensitivity issues.

Sample Code

async function handleEvent(event) {
try {
const request = event.request;
const url = new URL(request.url);
let pathname = url.pathname;

// Use regular expression for path replacement, replace /a or /a/xxx with /path-a or /path-a/xxx
if (pathname.startsWith('/a')) {
const aPathRegex = /^\/a(\/.*)?$/;
pathname = pathname.replace(aPathRegex, '/path-a$1');
}

// Path case conversion, directly convert the entire path to lowercase
if (pathname.startsWith('/b')) {
pathname = pathname.toLowerCase();
}

url.pathname = pathname;

// Create a new request
const newRequest = new Request(url.toString(), {
method: request.method,
headers: request.headers,
body: request.body,
redirect: 'manual'
});

const response = await fetch(newRequest);
return event.respondWith(response);
} catch (err) {
console.log(err);
}
}

addEventListener('fetch', event => {
handleEvent(event);
});


javascript

Example Preview

Enter a URL that matches the Edge function trigger rules in the browser address bar to preview the example effect.
Path regex replacement: Replace paths starting with /a with /path-a, while preserving the subsequent path structure.

Path case conversion: Convert the entire path to lowercase.