Apache HTTP Server Version 2.4
Available Languages: en |
mod_rewrite module uses a rule-based rewriting
engine, based on a PCRE regular-expression parser, to rewrite requested URLs on
the fly. By default,
mod_rewrite maps a URL to a filesystem
path. However, it can also be used to redirect one URL to another URL, or
to invoke an internal proxy fetch.
mod_rewrite provides a flexible and powerful way to
manipulate URLs using an unlimited number of rules. Each rule can have an
unlimited number of attached rule conditions, to allow you to rewrite URL
based on server variables, environment variables, HTTP headers, or time
mod_rewrite operates on the full URL path, including the
path-info section. A rewrite rule can be invoked in
httpd.conf or in
.htaccess. The path generated
by a rewrite rule can include a query string, or can lead to internal
sub-processing, external request redirection, or internal proxy
Further details, discussion, and examples, are provided in the
detailed mod_rewrite documentation.
RewriteBase directive specifies the
URL prefix to be used for per-directory (htaccess)
RewriteRule directives that substitute a relative
This directive is required when you use a relative path
in a substitution in per-directory (htaccess) context unless either
of the following conditions are true:
- The original request, and the substitution, are underneath the
(as opposed to reachable by other means, such as
- The filesystem path to the directory containing the
RewriteRule, suffixed by the relative
substitution is also valid as a URL path on the server
(this is rare).
- In Apache HTTP Server 2.4.11 and later, this directive may be
omitted when the request is mapped via
If you'd like to learn more about mod_rewrite, I recommend The Definitive Guide to Apache mod_rewrite by Rich Bowen. The online docs are also pretty good:
Apache Docs for RewriteRule · Apache mod_rewrite guide
Ниже приведены 5 примеров использования данного модуля:
1) Переделываем "product.php?id=12" в "product-12.html"
This website lets to generate detailed .htaccess mod rewrite rules as well as htaccess files, custom error pages, MIME types, Cache Control, Password Protect Directory for your blog or websites
Pattern: It is the pattern of request. The pattern is using regular expression (regex) syntax.
Logging mod_rewrite Activity
Once you’ve hooked up your mod_rewrite rules, you may still experience some issues depending on where you configured them. For instance, having to modify the paths when configuring them in httpd.conf vs. .htaccess. (httpd.conf being the more performant option if you have access to modify that configuration file)
Variables and flags
Variables and flags perform similar, but not quite the same functions for rules and conditions. Variables generally are near the beginning of the rule, and refer to where the parts of the incoming request. Flags are generally at the end of a rule, and indicate a response to a request.
These variables can be used when constructing a
RewriteCond and a
RewriteRule. A full list is available at
|Set to |
|Everything between the protocol (http:// or https://) and the request (|
|Everything after the server address — for example, a request to |
Please make sure you have
RewriteEngine On somewhere near the top of your .htaccess, before you attempt any rewrites.
I have found myself tripped up numerous times by leading slashes in URL rewriting systems. Whether they should be used in the pattern or in the substitution section of a RewriteRule or used in a RewriteCond statement is a constant source of frustration to me. This may be in part because I work with different URL rewriting engines, but I would advise being careful of leading slashes - if a rule is not working, that's often a good place to start looking. I never include leading slashes in mod_rewrite rules and always include them in ISAPI_Rewrite.
To redirect an old domain to a new domain:
To redirect all requests missing "www" (yes www):
To redirect all requests with "www" (no www):
Redirect old page to new page:
Hopefully if you've made it this far you now have a clear understanding of what URL rewriting is and how to add it to your site. It is worth taking the time to become familiar with - it can benefit your SEO efforts immediately, and increase the usability of your site.