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.
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.
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.
Access URL rewrite allows you to modify URLs that clients see and access, providing clean, SEO-friendly URLs while maintaining backend compatibility.
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.
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.
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 modifies URLs used for back-to-origin requests while keeping client-facing URLs unchanged.
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.
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
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 provides sophisticated URL rewrite capabilities supporting complex routing scenarios and conditional transformations.
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.
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.
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 provides advanced URL rewrite capabilities with support for dynamic expressions and complex transformation logic.
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.
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.
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
For scenarios requiring maximum flexibility, implementing Nginx-based reverse proxy solutions provides comprehensive control over URL rewrite operations.
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.
Configure Nginx rewrite directives for various scenarios:
server {
listen 80;
server_name cloud-example.net;
location / {
return 301 https://aliyun.com$request_uri;
}
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/old.com.crt;
location / {
return 301 https://aliyun.com$request_uri;
}
}
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;
}
}
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.
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.
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.
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.
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.
Wan2.2-S2V: AI Video Generation from Static Images on Alibaba Cloud
Alibaba Cloud Community - January 15, 2024
Alibaba Cloud Native Community - August 25, 2022
Alibaba Clouder - November 23, 2018
Alibaba Clouder - August 15, 2018
Alibaba Clouder - February 24, 2020
Alibaba Clouder - October 22, 2018
WAF(Web Application Firewall)
A cloud firewall service utilizing big data capabilities to protect against web-based attacks
Learn More
Networking Overview
Connect your business globally with our stable network anytime anywhere.
Learn More
CDN(Alibaba Cloud CDN)
A scalable and high-performance content delivery service for accelerated distribution of content to users across the globe
Learn More
Web App Service
Web App Service allows you to deploy, scale, adjust, and monitor applications in an easy, efficient, secure, and flexible manner.
Learn MoreMore Posts by Kidd Ip