×
Community Blog Alibaba Cloud Public Access Website Domain Rewrite

Alibaba Cloud Public Access Website Domain Rewrite

This article provides a step-by-step guide to configuring domain and URL rewrites for public websites on Alibaba Cloud using CDN, ALB, ESA, WAF, and Nginx.

I am working on something related to public website rewrite, would like to share it in this Blog as well

Implementing effective domain rewrite handling for public access websites on Alibaba Cloud requires mastering multiple service layers and their interconnected configurations. This comprehensive guide provides detailed step-by-step instructions for configuring URL rewrite and redirect mechanisms across Alibaba Cloud's ecosystem to optimize traffic routing, preserve SEO value, and enhance user experience.

Understanding Alibaba Cloud URL Rewrite Architecture

URL rewrite handling in Alibaba Cloud operates across multiple service layers, each serving specific purposes in the request processing pipeline. The architecture encompasses several key components working together to provide comprehensive traffic management capabilities.

CDN Layer URL Rewriting: Alibaba Cloud CDN provides both access URL rewrite and origin URL rewrite capabilities. Access URL rewrite modifies the URLs that clients see and access, while origin URL rewrite changes the URLs used for back-to-origin requests without affecting client-facing URLs. This dual approach allows you to maintain clean, SEO-friendly URLs while ensuring backend compatibility.

Application Load Balancer (ALB) Rewriting: ALB instances support advanced URL rewrite capabilities including host header modification, path rewriting, and query string manipulation based on configurable conditions. ALB can forward, redirect, and rewrite requests based on user-defined HTTP headers, cookies, and query strings.

Edge Security Acceleration (ESA) Rewriting: ESA services offer dynamic URL rewriting with support for both static and expression-based rewrite operations. ESA allows you to rewrite URLs at edge nodes, modifying path and query string components for origin fetch requests while keeping user access URLs unchanged.

Web Application Firewall (WAF) Integration: WAF services can be configured to work seamlessly with rewrite rules while maintaining security policies and protection mechanisms. WAF supports traffic redirection through DNS modifications while preserving security filtering capabilities.

Strategic Planning and Prerequisites

Infrastructure Assessment and Requirements

Before implementing URL rewrite configurations, conduct a comprehensive assessment of your current infrastructure and define clear objectives for your rewrite strategy.

Traffic Pattern Analysis: Examine current traffic patterns, identifying common URL structures, user navigation flows, and performance bottlenecks. Document existing URL schemes and map desired transformations to optimize user experience and search engine optimization.

Service Architecture Mapping: Document your current service architecture including CDN configurations, load balancer setups, backend server configurations, and existing rewrite rules. Identify potential conflicts and dependencies that may affect new implementations.

Performance Impact Assessment: Evaluate performance implications of URL rewrite operations, considering processing overhead, cache hit ratios, and origin server load. Plan rewrite rules to minimize performance impact while achieving desired functionality.

CDN-Based URL Rewrite Implementation

Access URL Rewrite Configuration

Access URL rewrite allows you to modify URLs that clients see and access, providing clean, SEO-friendly URLs while maintaining backend compatibility.

Step 1: Navigate to CDN Console and Domain Configuration

Access the Alibaba Cloud CDN console and locate your target domain name. In the left-side navigation tree, click "Cache" to access caching-related configurations, then select the "Access URL Rewrite" tab where you can create and manage rewrite rules.

Step 2: Create Access URL Rewrite Rules

Click "Create" to establish a new rewrite rule. Configure the "Path to Be Rewritten" parameter using Perl Compatible Regular Expressions (PCRE) syntax, ensuring the path starts with a forward slash and excludes protocol and domain name components. For example, use ^/old-category/(.*)$ to match legacy URL structures.

Set the "Target Path" to define the destination URL structure. When using the "Redirect" flag, you can include full URLs with protocol and domain names, such as https://new-domain.com/new-category/$1 where $1 references captured groups from the original path pattern.

Step 3: Configure Flag Parameters and Behavior

Select appropriate flag parameters to control rewrite behavior:

Redirect Flag: Implements HTTP 302 redirection, returning the new URL to clients and updating browser address bars. This preserves original URL parameters and allows cross-domain redirections.

Break Flag: Performs internal URL rewriting without client redirection, maintaining the original URL in the browser while serving content from the rewritten path. This cannot modify domain names or protocols.

enhance_redirect and enhance_break: Advanced options that modify both URL paths and query parameters, providing comprehensive rewrite capabilities for complex transformation requirements.

Origin URL Rewrite Configuration

Origin URL rewrite modifies URLs used for back-to-origin requests while keeping client-facing URLs unchanged.

Step 1: Access Origin URL Rewrite Settings

Navigate to the "Origin Fetch" section in your domain configuration, then select the "Origin URL Rewrite" tab. This configuration affects how CDN edge nodes request content from origin servers.

Step 2: Configure Origin Path Transformation

Create rewrite rules that transform client request paths to appropriate backend paths. Configure up to 50 origin URL rewrite rules for each domain name. The system processes rewrite rules in order from top to bottom.

Set flag parameters appropriately:

None or Break: Only rewrites the resource path in the URL

enhance break: Rewrites both resource path and query string components

Step 3: Implement Backend Server Compatibility

Ensure origin URL rewrite rules align with backend server configurations. Test rewrite rules against actual backend endpoints to verify proper functionality. Configure appropriate cache key strategies to ensure cached content aligns with rewritten URLs without creating conflicts.

Application Load Balancer URL Rewrite Implementation

ALB Advanced Rewrite Configuration

Application Load Balancer provides sophisticated URL rewrite capabilities supporting complex routing scenarios and conditional transformations.

Step 1: Access ALB Forwarding Rules Configuration

Navigate to the Server Load Balancer console and locate your ALB instance. Access the "Listeners" section and select the appropriate listener, then configure forwarding rules that support URL rewrite operations.

Step 2: Create Advanced Forwarding Rules with URL Rewrite

Configure forwarding rules using regular expression matching for flexible URL pattern recognition. Set path matching rules such as /api/v1/(.*)/(.*)/end to capture multiple URL segments for transformation.

The regular expression can contain only letters, digits, and specific special characters: . - _ / = ? ~ ^ * $ : ( ) [ ] + |. Implement rewrite actions using captured group references like /$1/$2 to maintain URL structure while modifying paths for backend routing.

Step 3: Configure Conditional Rewrite Logic

Implement conditional rewrite rules based on request headers, query parameters, or client characteristics. Create rules that evaluate conditions such as user agent strings, geographic location, or custom headers to determine appropriate rewrite actions.

ALB allows you to rewrite selected content of requests and responses, translating URLs, query string parameters, and modifying request and response headers. You can add conditions to ensure URLs or headers are rewritten only when certain conditions are met.

Edge Security Acceleration (ESA) Rewrite Implementation

ESA Dynamic URL Rewrite Configuration

Edge Security Acceleration provides advanced URL rewrite capabilities with support for dynamic expressions and complex transformation logic.

Step 1: Access ESA Website Configuration

Navigate to the ESA console and select your target website configuration. In the navigation pane, select the rewrite configuration section where you can create comprehensive rewrite rules.

Step 2: Configure Rule Conditions and Matching

Create rule expressions that define conditions for URL rewrite activation. Use components such as request path patterns, query string matching, header evaluation, and geographic location to control when rewrite rules apply.

Configure the "If requests match..." section to define conditions that requests must meet before rewrite operations execute.

Step 3: Implement Dynamic Rewrite Operations

Configure rewrite operations for both path and query string components:

Path Rewrite Options:

Keep: Retains the path from the original request without modification

Static Rewrite: Replaces the original request path with a fixed destination path

Dynamic Rewrite: Uses expressions to perform dynamic rewrite operations

Query String Rewrite Options:

Keep: Retains the query string from the original request

Static Rewrite: Replaces with a fixed destination query string

Dynamic Rewrite: Uses expressions for context-aware parameter manipulation

Nginx-Based Custom Rewrite Implementation

Self-Hosted Nginx Reverse Proxy Configuration

For scenarios requiring maximum flexibility, implementing Nginx-based reverse proxy solutions provides comprehensive control over URL rewrite operations.

Step 1: Nginx Installation and Basic Configuration

Install Nginx on your Alibaba Cloud ECS instance:

yum install nginx
sudo systemctl start nginx
sudo systemctl enable nginx

Configure basic server blocks and location directives to establish the foundation for custom rewrite rules.

Step 2: Implement Advanced Rewrite Rules

Configure Nginx rewrite directives for various scenarios:

HTTP to HTTPS Redirection:

server {
    listen 80;
    server_name cloud-example.net;
    location / {
        return 301 https://aliyun.com$request_uri;
    }
}

HTTPS to HTTPS Redirection:

server {
    listen 443 ssl;
    server_name example.com;
    ssl_certificate /path/to/old.com.crt;
    location / {
        return 301 https://aliyun.com$request_uri;
    }
}

Reverse Proxy with URL Preservation:

server {
    listen 80;
    server_name example.com;
    location / {
        proxy_pass http://aliyun.com;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Best Practices and Optimization Strategies

Performance Optimization Techniques

Rule Ordering and Processing Efficiency: Organize rewrite rules in order of specificity and frequency to minimize processing overhead. The system processes rules sequentially, so position more specific rules before general patterns to ensure correct matching behavior.

Cache Key Management: Ensure URL rewrite operations align with caching strategies to maintain optimal cache hit ratios. Configure cache keys that account for rewritten URLs while avoiding unnecessary cache fragmentation.

Redirect Loop Prevention: Implement safeguards to prevent infinite redirect loops. Common causes include origin server HTTP to HTTPS redirect rules conflicting with CDN configurations, creating continuous redirection cycles.

Security and Compliance Considerations

SSL/TLS Certificate Management: Coordinate SSL certificate configurations with domain rewrite operations. Ensure proper certificate validation and secure connection maintenance across rewritten domains. Configure force redirect to HTTPS when SSL certificates are properly configured.

Access Control Maintenance: Ensure URL rewrite operations maintain appropriate access controls and security policies. Review rewrite rules to prevent unintended exposure of restricted content or bypassing of security measures.

WAF Integration: Configure Web Application Firewall policies that work seamlessly with URL rewrite operations. Modify DNS records to redirect website traffic to WAF for protection while maintaining rewrite functionality.

Monitoring and Troubleshooting

Traffic Analysis and Metrics: Implement comprehensive monitoring for rewrite operations including success rates, response times, and error conditions. Track metrics for both client-facing and origin server interactions to identify performance bottlenecks.

Common Issue Resolution: Address frequent problems such as "too many redirects" errors by checking origin server configurations and ensuring rewrite rules don't create redirect loops. Verify that CDN origin protocols match server configurations to prevent redirection conflicts.

Testing and Validation Procedures: Establish testing protocols for rewrite rule deployment including functionality testing, performance validation, and security verification. Implement staging environments for rule testing before production deployment.

Advanced Integration Scenarios

Multi-Service Coordination

CDN and Load Balancer Integration: Coordinate URL rewrite operations between CDN and ALB services to ensure consistent behavior and optimal performance. Design rewrite strategies that leverage the strengths of each service layer while avoiding conflicts.

EdgeScript Custom Logic: Utilize Alibaba Cloud CDN's EdgeScript feature for advanced customization scenarios. EdgeScript supports custom authentication logic, request/response header modification, and complex rewrite operations based on business requirements.

Global Distribution Strategies: Implement URL rewrite configurations that support global content distribution and regional optimization. Configure rewrite rules that account for geographic variations and local compliance requirements.

Scalability and High Availability

Disaster Recovery and Failover: Design rewrite configurations that support disaster recovery scenarios and automatic failover procedures. Implement backup rewrite rules and alternative routing paths for service continuity.

Capacity Planning: Plan rewrite infrastructure capacity based on traffic patterns and growth projections. Monitor resource utilization across all service layers to ensure optimal performance and cost efficiency.

Effective domain rewrite handling requires careful coordination across multiple Alibaba Cloud services, systematic implementation of best practices, and ongoing optimization based on performance metrics and user feedback. Success depends on thorough planning, proper rule design, comprehensive testing, and continuous monitoring to maintain optimal performance and security standards.


Disclaimer: The views expressed herein are for reference only and don't necessarily represent the official views of Alibaba Cloud.

0 1 0
Share on

Kidd Ip

26 posts | 4 followers

You may also like

Comments