What Is mod_rewrite?
mod_rewrite is an Apache module that changes incoming URLs based on pattern-matching rules before the server decides how to handle the request. It is what lets a site show clean URLs like /products/42 instead of /index.php?id=42, force HTTPS, or redirect old pages to new ones. Rules are written with RewriteCond and RewriteRule directives, usually inside an .htaccess file on shared hosting.
The two core directives
A rewrite rule is built from two parts. RewriteCond is an optional condition that must be true for the rule below it to apply, such as checking whether a request is not already using HTTPS. RewriteRule does the actual work: it matches the requested URL against a regular expression and replaces it with a new target.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
This checks whether the connection is not HTTPS, and if so, rewrites any path (captured by (.*)$ and reinserted as $1) to the same path on the HTTPS version of the site, sending a permanent redirect.
Common rule flags
| Flag | Meaning |
|---|---|
| L | Last rule: stop processing further rules if this one matches |
| R or R=301 | Send an actual redirect to the browser (302 by default, or the status code specified) |
| NC | Case-insensitive match |
| QSA | Append the original query string to the new URL instead of dropping it |
| (no flag, internal rewrite) | Changes what Apache serves without the browser's address bar changing at all |
Internal rewrite vs. redirect
A rule without R performs an internal rewrite: Apache serves different content for the request, but the visitor's browser still shows the original URL and never knows a substitution happened. This is how clean, SEO-friendly URLs work: a request for /products/42 is quietly handed to index.php?id=42 behind the scenes.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^products/([0-9]+)$ index.php?id=$1 [L,QSA]
The two RewriteCond lines stop the rule from firing when the request already matches a real file or folder on disk, which avoids rewriting requests for actual images, CSS or existing pages.
Where mod_rewrite fits, and its Nginx equivalent
- On shared hosting with Apache or LiteSpeed, rewrite rules usually live in the site's
.htaccessfile and apply without a server restart. - On a VPS running Apache, rules can also go directly in the virtual host configuration, which performs slightly better since Apache does not have to search for .htaccess files on every request.
- Nginx has no mod_rewrite and ignores .htaccess entirely; the same logic is expressed with its own
rewritedirective or, more often, atry_filesblock.
# The same front-controller pattern on Nginx
location / {
try_files $uri $uri/ /index.php?$args;
}
Frequently asked questions
What does mod_rewrite actually do?
It is an Apache module that matches incoming URLs against pattern rules and either rewrites them internally to a different file, or sends the browser an actual redirect to a new URL. Most CMS platforms use it to create clean, readable page addresses.
Why is my RewriteRule not working?
The most common causes are that mod_rewrite is not enabled on the server, AllowOverride is not set to allow .htaccess rules for that folder, or RewriteEngine On is missing from the top of the rules. Checking the Apache error log usually points to which one applies.
What is the difference between a rewrite and a redirect?
A rewrite changes what the server serves internally while the browser's address bar stays the same. A redirect, marked with the R flag, tells the browser to request a completely new URL, which it then shows in the address bar.
Do I need mod_rewrite for WordPress permalinks?
Yes, on Apache or LiteSpeed, pretty permalinks rely on mod_rewrite rules that WordPress writes automatically into .htaccess. If the module is disabled on the server, only the default ?p=123 style links will work.
Can I use mod_rewrite rules on Nginx?
No, Nginx does not read .htaccess files or support mod_rewrite syntax at all. The same rewriting logic has to be rewritten using Nginx's own rewrite and try_files directives.
Is mod_rewrite bad for website performance?
A small number of straightforward rules has a negligible performance cost. A very large or inefficient set of regular expressions evaluated on every request can add measurable overhead, which is one reason high-traffic sites sometimes move rules into the main server configuration instead of .htaccess.
Related topics
- What Is Apache (Apache HTTP Server)? Web Servers
- What Is an SSL Certificate? Security & SSL
- What Is a .htaccess File? Web Servers
- What Is Shared Hosting? Hosting Basics
- 301 vs. 302 Redirect Errors & Troubleshooting