No Login Data Private Local Save

Apache RewriteRule Generator - Online mod_rewrite Helper

7
0
0
0

Apache RewriteRule Generator

Generate mod_rewrite rules for your .htaccess file. Choose a template or build custom rewrite rules with conditions.

301 Redirect
Old URL → New URL
Force HTTPS
HTTP → HTTPS
WWW Redirect
non-WWW → WWW
Remove WWW
WWW → non-WWW
Hide .php
Clean URLs
Remove index.php
Framework clean URLs
Slug Rewrite
/post/123 → post.php?id=123
Custom Rule
Start from scratch
Rule Configuration
Regular expression pattern matching the request URL path (without query string). Docs
^page\.html$ ^blog/([a-z0-9-]+)/?$ ^product/([0-9]+)/?$ .* (match all) ^index\.php$
Use $1, $2 for back-references to captured groups. Use %{HTTP_HOST}, %{REQUEST_URI} for server variables. For absolute URLs, include the full URL like https://example.com$1.
/new-page.html /blog/$1/ Full HTTPS URL /index.php?slug=$1 - (passthrough)
[L] Last [R=301] Permanent [R=302] Temporary [NC] No Case [QSA] Append Query [NE] No Escape [PT] Pass Through
[L] stops processing further rules. [R] redirects (only one redirect flag allowed). [NC] makes matching case-insensitive. [QSA] preserves the original query string. Click to toggle.
Conditions must be met before the RewriteRule is applied. Multiple conditions use AND logic by default. Add [OR] flag to change logic. Learn more
Generated Rule
# Enable Rewrite Engine
RewriteEngine On

# Generated RewriteRule
RewriteRule ^old-page\.html$ /new-page.html [L]
What This Does

Matches requests for old-page.html and internally rewrites them to /new-page.html. The [L] flag tells Apache to stop processing further rewrite rules after this one matches.

Frequently Asked Questions

What is mod_rewrite and how does it work?

mod_rewrite is an Apache module that allows you to manipulate URLs using regular expressions. It works by intercepting incoming HTTP requests and applying pattern-matching rules defined in .htaccess files or Apache configuration. When a request URL matches a RewriteRule pattern, Apache transforms (rewrites or redirects) the URL based on the substitution you specify. Optional RewriteCond directives add conditions that must be satisfied before a rule is executed, giving you fine-grained control over when and how URLs are rewritten.

What's the difference between RewriteRule and RewriteCond?

RewriteRule is the core directive that performs the actual URL transformation. It takes a regex pattern (matching the request path) and a substitution string. RewriteCond (Rewrite Condition) is an optional directive that precedes a RewriteRule and adds prerequisites. For example, a RewriteCond can check if the request is NOT coming from a specific host, or if a file does NOT exist on the server, before allowing the rule to execute. Multiple RewriteCond directives are combined with AND logic by default; use the [OR] flag to change this behavior.

How do I redirect all HTTP traffic to HTTPS?

Use a RewriteCond to check the %{HTTPS} variable and a RewriteRule to perform the redirect. Select the "Force HTTPS" template above, or manually configure:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

This checks if HTTPS is off and permanently redirects (301) to the HTTPS version while preserving the full URL path and query string.

What does the [L] flag do and why is it important?

The [L] (Last) flag tells Apache to stop processing any subsequent rewrite rules in the current context after the current rule matches. Without it, Apache continues evaluating later rules, which could unintentionally modify the URL further. However, note that in .htaccess files, the [L] flag does not completely stop all processing — Apache still re-injects the rewritten URL and starts the rule evaluation again from the top. This is why combining [L] with proper conditions is essential to avoid infinite redirect loops.

How can I remove .php extensions from my URLs?

To hide .php extensions, you need two rules: one to internally rewrite clean URLs to the actual PHP files, and optionally another to externally redirect direct .php URLs to the clean version. Select the "Hide .php" template or use:

RewriteEngine On
# Internally rewrite /page to /page.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-]+)/?$ $1.php [L]

The RewriteCond checks if a corresponding .php file actually exists before rewriting, preventing 404 errors.

How do I debug mod_rewrite rules when something goes wrong?

Debugging mod_rewrite can be challenging. Here are the most effective approaches:

  1. Enable RewriteLog (Apache 2.2 and earlier): RewriteLogLevel 3 in your config.
  2. Use LogLevel (Apache 2.4+): Add LogLevel alert rewrite:trace3 to see detailed rewrite processing in the error log.
  3. Test with .htaccess tester tools (like this generator paired with online sandboxes).
  4. Add temporary redirect rules with unique markers to isolate which rule is (or isn't) matching.
  5. Check Apache error logs — they often contain clues about syntax errors or infinite redirect loops.
What are the most common .htaccess rewrite mistakes?

Common pitfalls include:

  • Forgetting RewriteEngine On — rules won't work without it.
  • Infinite redirect loops — caused by rules that keep matching after rewriting (use [L] and add exclusion conditions like RewriteCond %{ENV:REDIRECT_STATUS} ^$).
  • Not escaping dots in patterns — . matches any character in regex; escape literal dots as \..
  • Using ^(.*)$ without anchoring — can match unexpected paths; always anchor with ^ and $.
  • Mixing up $1 and %1 back-references — $N refers to RewriteRule captures, %N refers to RewriteCond captures.
What's the difference between internal rewrite and external redirect?

An internal rewrite (no [R] flag) changes the URL server-side only — the visitor's browser address bar remains unchanged. This is used for clean URLs and framework routing. An external redirect (with [R=301] or [R=302]) sends a new URL back to the browser, which then makes a new request — the address bar updates. Use external redirects for SEO-critical URL migrations (301 permanent) and temporary moves (302). Use internal rewrites when you want friendly URLs without changing what the visitor sees.