Overview
Therewrite middleware transforms URL paths internally without sending redirects to the client. The browser URL remains unchanged while the server processes a different path.
Installation
Quick Start
Examples
Prefix Rewrite
Regex Rewrite
Multiple Rules
Version Stripping
API Reference
Rule Type
Rewrite vs Redirect
| Feature | Rewrite | Redirect |
|---|---|---|
| Client sees | Original URL | New URL |
| HTTP round trip | No | Yes |
| SEO | Hidden | Visible |
| Use case | Internal routing | URL changes |
Technical Details
Implementation Overview
The rewrite middleware operates by modifying theRequest.URL.Path before the request reaches downstream handlers:
- Rule Compilation: Regex patterns are compiled once during middleware initialization to optimize performance
- Path Matching: Each incoming request’s path is evaluated against rules in order
- First Match Wins: Processing stops at the first matching rule
- Path Replacement: The request URL path is modified in-place
Rule Processing
Prefix Rules:- Uses
strings.HasPrefix()for fast prefix matching - Strips the matched prefix and prepends the replacement
- Example:
/old/pathwith rulePrefix("/old", "/new")becomes/new/path
- Compiled using Go’s
regexppackage during initialization - Supports capture groups (
$1,$2, etc.) for dynamic replacements - Uses
ReplaceAllString()for substitution - Example:
/user/123/profilewith pattern^/user/(\d+)/profile$and replacement/profiles/$1becomes/profiles/123
Performance Characteristics
- Initialization: O(n) where n is the number of regex rules
- Per-Request: O(r) where r is the number of rules (worst case: no match)
- Memory: Compiled regex patterns are cached
- Early Exit: Processing stops after first match for efficiency
Internal State
The middleware maintains:- Compiled regex patterns in the
Rule.refield - Original rule configuration in the
Optionsstruct - No request-specific state (thread-safe)
Best Practices
Rule Ordering
Place more specific rules before general ones:Performance Optimization
- Minimize the number of regex rules when possible
- Use prefix rules for simple path transformations
- Test regex patterns for efficiency with large inputs
Security Considerations
- Validate rewrite patterns to prevent unintended path access
- Be cautious with user-controlled input in dynamic rules
- Consider path traversal implications when rewriting paths
Testing
The rewrite middleware includes comprehensive test coverage for various scenarios:| Test Case | Description | Expected Behavior |
|---|---|---|
TestNew | Basic prefix rewrite | Rewrites /old/path to /new/path using prefix rule |
TestWithOptions_Regex | Regex pattern matching | Matches regex pattern with version number and resource path |
TestWithOptions_MultipleRules | Multiple rule processing | Applies first matching rule and stops processing |
TestWithOptions_NoMatch | Path without matching rule | Leaves path unchanged when no rules match |
TestPrefix | Prefix rule creation | Creates rule with correct match, rewrite, and regex=false |
TestRegex | Regex rule creation | Creates rule with pattern, replacement, and regex=true |
TestWithOptions_RegexCapture | Regex capture groups | Replaces /user/123/profile with /profiles/123 using capture group |