URL Rewrite Tester
Test regex-based URL rewrite rules for Apache, Nginx, and IIS before deploying to production.
Back to all tools on ToolForge
Rewrite Result
About URL Rewrite Tester
This URL rewrite tester validates regex-based rewrite rules by testing pattern matching and replacement transformations before deploying to Apache, Nginx, or IIS servers.
It is useful for testing mod_rewrite rules, Nginx location blocks, IIS URL Rewrite configurations, debugging complex regex patterns, verifying capture group extraction, preventing production URL breaks, and ensuring clean URL routing works correctly before server configuration changes.
How URL Rewrite Rules Work
Rewrite rules follow a three-step process:
- Pattern Matching: The regex pattern is tested against the source URL path
- Capture Extraction: Parentheses groups (capture groups) extract matching values
- Replacement: The target string uses $1, $2, etc. to insert captured values
Example Rewrite Rule: Source URL: /blog/123 Pattern: ^/blog/(\d+)$ Target: /posts?id=$1 Step 1: Match pattern against /blog/123 ✓ Step 2: Capture group (\d+) extracts "123" Step 3: Replace $1 with "123" → /posts?id=123 Result: /blog/123 → /posts?id=123
Common Rewrite Pattern Examples
| Use Case | Pattern | Target | Example Match |
|---|---|---|---|
| Blog Post ID | ^/blog/(\d+)$ | /post?id=$1 | /blog/456 → /post?id=456 |
| User Profile | ^/user/([a-z]+)$ | /profile.php?name=$1 | /user/john → /profile.php?name=john |
| Category/Slug | ^/([a-z]+)/([a-z0-9-]+)$ | /$1.php?slug=$2 | /products/blue-widget → /products.php?slug=blue-widget |
| Date Archive | ^/archive/(\d{4})/(\d{2})$ | /archive.php?year=$1&month=$2 | /archive/2024/03 → /archive.php?year=2024&month=03 |
| Remove Extension | ^(.+)\.html$ | $1 | /about.html → /about |
| Force HTTPS | ^(.*)$ | https://example.com$1 | HTTP → HTTPS redirect |
Regex Pattern Reference
| Pattern | Matches | Example |
|---|---|---|
| \d+ | One or more digits | 123, 4567, 0 |
| [a-z]+ | One or more lowercase letters | hello, world, abc |
| [a-zA-Z]+ | One or more letters (any case) | Hello, WORLD, abc |
| [a-z0-9-]+ | Slug-style strings | my-page, item-123 |
| \d{4} | Exactly 4 digits | 2024, 1999 |
| .* | Any characters (greedy) | matches everything |
| ^ and $ | Start and end anchors | ensures exact match |
| ? | Optional (0 or 1) | colou?r matches color/colour |
Apache mod_rewrite Example
# Enable rewrite engine
RewriteEngine On
# Clean URL for blog posts
RewriteRule ^blog/([0-9]+)$ /post.php?id=$1 [L,QSA]
# Clean URL for user profiles
RewriteRule ^user/([a-zA-Z0-9_-]+)$ /profile.php?username=$1 [L,QSA]
# Remove .html extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
# Force HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Nginx Rewrite Example
# Enable rewrite in server block
server {
listen 80;
server_name example.com;
# Blog post clean URLs
rewrite ^/blog/([0-9]+)$ /post.php?id=$1 last;
# User profile URLs
rewrite ^/user/([a-zA-Z0-9_-]+)$ /profile.php?username=$1 last;
# Remove .html extension
location ~ ^(.+)\.html$ {
if (-f $document_root$1) {
rewrite ^(.+)\.html$ $1 last;
}
}
# Force HTTPS
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
}
Common Use Cases
- Clean URLs: Convert /page.php?id=123 to /page/123 for better SEO
- Migration: Redirect old URL structures to new ones during site redesign
- Canonicalization: Force www vs non-www, HTTP vs HTTPS, trailing slashes
- Language Routing: Route /en/, /es/, /fr/ to appropriate language handlers
- API Versioning: Route /api/v1/users to version-specific handlers
- Legacy Support: Maintain old URLs while migrating to new structure
Rewrite vs Redirect
| Aspect | Rewrite (Internal) | Redirect (External) |
|---|---|---|
| Browser URL | Unchanged | Changes to new URL |
| Server Processing | Internal path mapping | HTTP 3xx response |
| SEO Impact | Original URL indexed | New URL indexed (301) |
| Use Case | Clean URLs, routing | Domain changes, moved pages |
Testing Checklist
Before deploying rewrite rules: □ Pattern matches expected URLs □ Pattern does NOT match unrelated URLs □ Capture groups extract correct values □ Replacement produces valid target URLs □ No infinite rewrite loops □ Query parameters preserved (QSA flag) □ Special characters properly escaped □ Case sensitivity is correct □ Anchors (^ and $) used appropriately □ Tested with edge cases (empty, special chars)
Frequently Asked Questions
- How do URL rewrite rules work?
- URL rewrite rules use regular expressions to match incoming URL paths and transform them into different paths. The pattern (regex) matches the source URL, capturing groups extract values, and the replacement string uses backreferences ($1, $2) to insert captured values into the new URL. This enables clean URLs like /blog/123 to internally route to /posts?id=123.
- What is the difference between rewrite and redirect?
- Rewrite changes the URL internally—the server processes a different path but the browser URL stays unchanged. Redirect sends an HTTP 3xx response telling the browser to request a different URL, changing what users see in the address bar. Rewrite is server-side; redirect is client-side.
- What do capture groups like (\d+) mean in rewrite patterns?
- Parentheses create capture groups that extract matching values. \d+ matches one or more digits. In the pattern ^/blog/(\d+)$, the parentheses capture the ID number. The replacement string references it as $1 (first group), $2 (second group), etc. Example: /blog/123 matches with $1=123, rewritten to /posts?id=$1 becomes /posts?id=123.
- What are common URL rewrite patterns?
- Common patterns include: ^/blog/(\d+)$ → /post?id=$1 (numeric ID), ^/user/([a-z]+)$ → /profile.php?name=$1 (username slug), ^/products/(\d+)/(\d+)$ → /products.php?cat=$1&id=$2 (multiple parameters), ^/archive/(\d{4})/(\d{2})$ → /archive.php?year=$1&month=$2 (date-based URLs).
- Why test rewrite rules before deploying?
- Incorrect rewrite rules can break all URLs on a site, cause infinite redirect loops, expose sensitive paths, or create duplicate content SEO issues. Testing validates that patterns match expected URLs and produce correct replacements before changes affect production traffic and search engine crawling.
- What is the difference between mod_rewrite and Nginx rewrite syntax?
- Apache mod_rewrite uses RewriteRule with flags like [L,R=301,QSA]. Nginx uses rewrite directive with flags like last, break, redirect. Apache uses $1, $2 for backreferences; Nginx also uses $1 but in replacement uses different syntax. Apache .htaccess paths are relative; Nginx location blocks match absolute paths.