URL rewriting module for the Apache web server. It is commonly used for so-called `pretty URLs` , but also provides the power and flexibility to perform various request handling tasks beyond simple substitutions.
Introduction to mod_rewrite
mod_rewrite is an apache module which allows requests to be transformed server-side. It can be used to perform both internal and external redirects, and can provide conditional hooks into functionality provided by other modules (like mod_proxy). Comprehensive reference and supplementary documentation with several examples are available from the Apache documentation website, in addition to the many solutions to common and advanced mod_rewrite issues available here.
A comprehensive introduction into mod_rewrite
Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask?
Common mod_rewrite usage scenarios and questions
- How do I make a URL like "/page" redirect to a script such as "/index.php/page"?
- How to hide the .html extension with Apache mod_rewrite
- How do I force users to visit my domain name with "www"?
- How do I force users to visit my domain name without "www"?
- How do I redirect my subdomain to a folder?
- How do I append the existing query string to my rewritten URL?
- How do I transform my URL path segments into query string key-value pairs?
- Why isn't the L flag working in my .htaccess file?
- Why aren't environment variables working in my test patterns?
- CSS, JS and images do not display with pretty url
- I want to change my Wordpress url, but I get a Wordpress 404 Not Found error
- How do I redirect an ugly url to a pretty url, then rewrite it back to the ugly url, without causing an infinite loop of redirects
- How do I rewrite a name to an id? (where the id is not in the requested url)
- How do I combine two redirects into one?
How does mod_rewrite work?
mod_rewrite works its magic by applying a series of rules and conditions to the requested URL, performing substitutions when matching succeeds. The majority of this work is done by the RewriteRule
and RewriteCond
directives which can be defined in server (or virtual host) configurations or in .htaccess files. This allows mod_rewrite to be used in a variety of deployment scenarios, including shared hosting where it is often installed by default.
Using the directives and variables* available, it's possible to condition rewrites on a reasonably complex set of conditions - including negated regular expressions and literal matches. As a simple example to demonstrate some of these features, consider performing an internal redirect to an alternate index file if a subdomain was requested:
RewriteEngine On # Turn on the engine
RewriteCond %{HTTP_HOST} =s.example.com # Test for subdomain
RewriteCond %{REQUEST_URI} !^/.+$ # Test if the request URI is empty
RewriteRule ^ /index-alt.html [QSA] # Internal rewrite with QSA flag*
*A full list of variables is available in the RewriteCond
section of the documentation. Additionally, a list of rule flags is available in the RewriteRule
section.
mod_rewrite is not without its quirks though, especially when it comes to differences between defining rules in a server configuration and in a .htaccess
file. When possible, use the RewriteLog
directive to see what's going on under the hood, and when in doubt, ask the Stack Overflow community!
Do I need mod_rewrite?
mod_rewrite is a complex and powerful tool. As such, for some simple use cases there are simpler and occasionally faster alternatives. The Apache wiki documents some of these use cases in their When Not To Use Rewrite page.
Asking a new mod_rewrite question
There are many users here who are happy to help demystify mod_rewrite. To get the best possible answers, it's recommended that questions include all of the rules, a mention of whether they are defined in httpd.conf or .htaccess, an example of what should happen (but doesn't; e.g. *"`/home` should go to `home.php`"*), and, finally, a description of what "doesn't work" about it (for instance, *"Apache returns a 404 error message"*). Happy rewriting!Resources and Links
- Mod Rewrite Generator - Generateit.net: It offers simple online interface to generate customized rewrite rules for common situations.
- htaccess tester: To test your htaccess code including mod-rewrite code.