Updated November 2026 for Apache 2.4. The complete, current reference is Advanced Mod_Rewrite in THE Ultimate Htaccess.
Welcome to the world of mod_rewrite, the powerhouse module within the Apache HTTP Server that gives you the flexibility to manipulate URLs like a pro. In this guide, we'll dive into the essentials of mod_rewrite and explore how .htaccess files can transform the way you manage your website's URLs. Whether you're looking to create SEO-friendly URLs, implement redirects, or build complex URL patterns, this guide will help you understand the core concepts and best practices.
mod_rewrite is an Apache module that allows you to rewrite URLs on the fly, altering the structure of incoming requests to meet specific rules. It's commonly used to create user-friendly URLs, enforce canonical links, implement redirects, and manage various aspects of web traffic. With mod_rewrite, you can take control of how users and search engines interact with your website.
.htaccess files are configuration files that allow you to apply specific rules to individual directories on your server without modifying the main Apache configuration. This is useful for shared hosting environments where you may not have access to the global Apache configuration, or when you want to manage rules at a finer granularity.
To get started with mod_rewrite in .htaccess files, let's cover the basics:
RewriteEngine: This directive enables or disables mod_rewrite. To use mod_rewrite, you need to ensure this is set to "On". RewriteRule: This directive defines a specific rule for rewriting URLs. It takes a pattern and a replacement string, along with optional flags that modify the behavior of the rule. RewriteCond: This directive allows you to add conditions to your rules. It specifies under what circumstances a RewriteRule should be applied.
Here's a basic example of using mod_rewrite to create a user-friendly URL:
RewriteEngine On
RewriteRule ^products/([0-9]+)$ product.php?id=$1 [L]
In this example, we enable mod_rewrite with RewriteEngine On and then define a rule that rewrites any URL matching products/123 to product.php?id=123. The [L] flag stops processing for this pass. In .htaccess, though, the rewritten URL goes through the whole file again. Apache 2.4 added [END], which stops for good and is the cure for most rewrite loops.
mod_rewrite is also commonly used for implementing redirects, such as redirecting old URLs to new ones or enforcing HTTPS:
RewriteEngine On
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
Any request that is not already for https://www.example.com (plain HTTP, the other hostname, anything else) goes to https://www.example.com in a single 301 (permanent) redirect, instead of two separate hops for HTTPS and the hostname.
Sometimes, you need more complex conditions to manage specific cases. RewriteCond allows you to specify additional checks before applying a RewriteRule. For example, you can switch on maintenance mode just by creating a flag file, while your own IP still gets through:
ErrorDocument 503 /maintenance.html
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/maintenance.flag -f
RewriteCond %{REMOTE_ADDR} !=203.0.113.10
RewriteCond %{REQUEST_URI} !=/maintenance.html
RewriteRule ^ - [R=503,L]
Header always set Retry-After "3600" "expr=%{REQUEST_STATUS} == 503"
Create maintenance.flag to turn it on and delete it to turn it off. Visitors get a real 503 with Retry-After, so search engines come back later instead of indexing the "be right back" page.
Test Thoroughly: Always test your .htaccess rules in a development environment before deploying them to production. Mistakes in mod_rewrite can break your site. Test permanent redirects as [R=302] first, because browsers cache a 301 aggressively. To watch mod_rewrite make its decisions, set LogLevel alert rewrite:trace3 in the server config (it replaced RewriteLog in 2.4).
Use Flags Wisely: Flags like [L], [R=301], and [NC] can have significant effects on your rewrite rules. Make sure you understand their implications.
Minimize Performance Impact: Complex rewrite rules can impact server performance. Keep your rules as simple as possible and avoid excessive use of conditions.
Security Considerations: Be cautious when rewriting URLs to prevent security vulnerabilities like open redirects or unauthorized access to sensitive files.
mod_rewrite in .htaccess files is a powerful tool that opens up a world of possibilities for managing URLs and controlling web traffic. With the flexibility to implement SEO-friendly URLs, redirects, and complex URL patterns, it's an essential skill for webmasters and developers. By following best practices and testing your rules thoroughly, you can harness the full power of mod_rewrite to create an optimized and user-friendly website.