Detailed description of the. htaccess Rewrite rules

Source: Internet
Author: User
Tags deprecated parent directory response code

Detailed description of the. htaccess Rewrite rules

Author Freemouse July 22, 2010

With Apache virtual host a lot of friends, Apache provides. The htaccess module can set rewrite rules for each virtual host, which is useful for SEO optimization of the site and also improves the user experience. Domestic virtual machines are generally not available. Htaccess features (as far as I know, Discuz's hosts seem to provide this capability), while in foreign hosts,. htaccess functions seem to be standard, the author's blog stand on Mt, support. htaccess, every time I see a bunch of people writing. htaccess Settings, very Many commands are unclear, viewing, modification is very inconvenient, learn from the bitter, painstaking learning, know its why ~

Learning premise: (No friend to learn, to better understand the following text)

    • Linux Basics (It's not going to be okay, write a. htaccess no need to make a lot of trouble, recommended: Bird's-brother private Cuisine Linux Foundation)
    • Regular expressions (rewrite rules are built on regular basis, recommended: Regular expression 30-minute introductory tutorial)

Syntax format for rewrite:

    1. Rewriteengine on #要想rewrite起作用, it has to be written.
    2. Rewritebase Url-path #设定基准目录, for example, if you want to rewrtie a file under the root directory, is "/"
    3. Rewritecond test-string Condpattern #写在RewriteRule之前, can have one or n, used to test the matching conditions of the rewrite, specifically how to write, the following will be said in detail.
    4. Rewriterule Pattern Substitution #规则

Rewriteengine on| Off

The rewriteengine is used to turn on or deactivate the rewrite function.
Rewrite configurations does not automatically inherit, so you have to add this instruction to each virtual host directory that you want to use rewrite functionality.

Rewritebase Url-path

The rewritebase is used to set the overridden base URL. In the following, you can see that rewriterule can be used in a directory-level configuration file (. htaccess) and work locally, where the rules actually deal with just a portion of the local path prefix being stripped. After processing, the path is automatically attached back. The default value is "Rewritebase physical-directory-path".
When a new URL is replaced, the module must re-inject the URL into the server processing. To do this, it must know its corresponding URL prefix or a URL reference. Typically, this prefix is the corresponding file path. However, most web site URLs do not correspond directly to their physical file paths, and therefore are not generally allowed to do so! So in this case, you must use the Rewritebase directive to specify the correct URL prefix.
If your site server URL does not correspond directly to the physical file path and you need to use the Rewritebase directive, you must specify Rewriterule in each of the corresponding. htaccess files.

Rewritecond teststring Condpattern [flags]

The Rewritecond directive defines the condition of a rule, that is, to have one or more rewritecond directives before a rewriterule instruction. The override rules after the condition will only work if the current URI matches the pattern and meets these criteria.
TestString is a plain-text string, but it can also contain the following extensible components:

    1. Rewriterule Reverse Reference: The Reference method is $N (0 <= N <= 9) refers to the current (with a number of rewritecond instructions) Rewriterule in the grouping element matching the pattern (parentheses!).
    2. Rewritecond Reverse Reference: The Reference method is%N (1 <= N <= 9) referencing the grouping component in the last qualifying condition in several rewritecond conditions (parentheses!).
    3. Rewritemap Extension: Reference method is ${mapname:key|default}
    4. Server variables: Reference method is%{name_of_variable} This is the most common feature we use.

name_of_variable Specific values are shown in the following table:

HTTP headers: Connection & Reque ST:  
Http_forwa rded
Server Internals: system stuff: Specials:
SERVER _software

These correspond to similar named HTTP MIME headers, c variables for Apache servers, and struct TM fields in Unix systems, most of which are described in other manuals or CGI specifications. The variables that are unique to mod_rewrite are:
If the request being processed is a child request, it contains the string "true", otherwise it is "false". module in order to parse additional files in the URI, it is possible to generate a child request.
This is the version of the Apache module API in use in HTTPD (the internal interface between the server and the module), which is defined in Include/ap_mmn.h. This version of the module corresponds to the version of Apache being used (for example, in the release version of Apache 1.3.14, this value is 19,990,320:10). In general, it is the author of the module that is interested in it.
This is the full HTTP request line sent by the browser to the server. (For example, "get/index.html Http/1.1″"). It does not contain any additional header information sent by the browser.
This is the resource requested in the HTTP request line. (such as "/index.html" in the example above.)
This is the file path name or description of the complete local file system that matches the request.

Condpattern is a conditional pattern, that is, a regular expression applied to the current instance teststring, that is, teststring will be evaluated and then matched with Condpattern.

Note:Condpattern is a Perl-compatible regular expression , but there are several additions:

    1. You can use the ' character (exclamation point) in the pattern string ! to achieve a matching reversal.
Rewriteoptions Options

RewriteOptionsDirectives set some options for the current server-level and directory-level configurations. Option can be one of the following values:

This value forces the current configuration to inherit its parent configuration. In a virtual host-level configuration, it means that the primary server's mapping tables, conditions, and rules can be inherited. In a directory-level configuration, it means that the .htaccess conditions and rules in its parent directory can be inherited.
to avoid an endless internal redirect at the directory level, the RewriteRule mod_rewrite processing of this request is stopped when such redirects and 500 internal server errors reach a maximum value. If you do need to allow internal redirects greater than 10 times for each request, you can increase this value.
Rewriterule Pattern Substitution [flags]

RewriteRuleThe instruction is the root of the rewrite engine. This instruction can be used multiple times. Each directive defines a simple rewrite rule. The order in which these rules are defined is particularly important because, at run time, the rules are enforced in this order.

Pattern is a Perl-compatible regular expression that acts on the current URL.

In addition, you can use the ! pattern prefix of the no character (') to achieve the reversal of pattern. However, it is important to note that when you use the no character to invert pattern, you cannot use a grouped wildcard component in pattern, that is, $n.

The Substitution in the rewrite rule is a string that is substituted (or replaced) when the original URL matches the Pattern . In addition to plain text, you can also use

    • $NReverse reference to the pattern of rewriterule
    • %NInverse reference to the last matching rewritecond pattern
    • Server variables in the rule condition test string ( %{VARNAME} )
    • Map function Calls (${mapname:key|default})

Here are a few complete examples for you to refer to:

First, anti-theft chain function
Only the four lines of the implementation of the anti-theft chain is not very magical ^_^, writing is not and feel complex.
Rewriteengine on
Rewritecond%{http_referer}!^http://(. +.)?[NC]
Rewriterule. *. (jpe?g|gif|bmp|png) $/images/nohotlink.jpg [L]

Second, the website normalization
This is an example of redirecting all two-level domain names to, so is it not so easy now?
Options +followsymlinks
Rewriteengine on
Rewritecond%{http_host} ^ [NC]
Rewriterule ^ (. *) $$1 [r=301,l]

Third, the temporary error page
When your site is upgraded and modified, you'd better let the visitor go to the specified page instead of the unfinished page or the error page.
Rewriteengine on
Rewriterule $/error.html [r=302,l]

Iv. redirect RSS address to Feedsky
In addition to changing the RSS address in the template, the. htaccess also makes it easier to change the RSS address.
Rewriteengine on
Rewritecond%{http_user_agent}! FeedSky [NC]
Rewritecond%{http_user_agent}! Feedvalidator [NC]
Rewriterule ^feed/? ([_0-9a-z-]+)?/?$

Appendix: Flags

  1. ' Redirect| R [=code] ' (forced redirection redirect)

    A substitution that is prefixed with http://thishost[:thisport]/(making the new URL a URI) can be forced to perform an external redirect. If code is not specified, an HTTP response code of 302 (temporary move) is generated. If you need to use a different response code in the range of 300-400, you can specify this value here, plus one of the following symbol names: Temp (default), permanent, seeother. It can be used to feedback the normalized URL to the client, such as rewriting "/~" to "/u/", or/u/user plus slashes, etc. Note: When using this tag, you must ensure that the replacement field is a valid url! Otherwise, it will point to an invalid location! And keep in mind that this tag itself is just a prefix to the URL plus http://thishost[:thisport]/, and the rewrite operation will still continue. In general, you will want to stop the rewrite operation and immediately redirect, then you also need to use the ' L ' tag.

  2. ' Forbidden| F ' (force URL for forbidden Forbidden)

    Forces the current URL to be disabled, that is, immediately feedback an HTTP response code of 403 (forbidden). With this tag, you can link several rewriteconds to conditionally block certain URLs.

  3. ' Gone| G ' (force URL to obsolete gone)

    Forces the current URL to be obsolete, that is, immediately feedback an HTTP response code 410 (deprecated). Using this tag, you can indicate that the page has been deprecated and does not exist.

  4. ' Proxy| P ' (Force agent proxy)

    This flag causes the replacement ingredient to be internally coerced to the proxy request and immediately (that is, the rewrite rule handles immediate interrupts) handing over the processing to the proxy module. You must make sure that this replacement string is a valid URI (such as a common one that starts with http://hostname) that can be handled by the Apache proxy module. With this tag, some remote components can be mapped to the local server namespace, thereby enhancing the functionality of the Proxypass directive. Note: To use this feature, the proxy module must be compiled on the Apache server. If you are not sure, you can check the output of "httpd-l" for mod_proxy.c. If so, mod_rewrite can use this feature, and if not, you must enable Mod_proxy and recompile the HTTPD program.

  5. ' Last| L ' (Final Rule last)

    Stops the rewrite operation immediately and no longer applies another rewrite rule. It corresponds to the last command in Perl or the break command in the C language. This tag prevents the currently overridden URL from being overridden by its successor rule. For example, use it to rewrite the URL of the root path ('/') to a URL that actually exists, for example, '/e/www/'.

  6. ' Next| N ' (re-execute next round)

    Re-executes the rewrite operation (starting with the first rule). The URL that was processed again is not the original URL, but the URL that was processed by the last rewrite rule. It corresponds to the next command in Perl or the Continue command in the C language. This tag can restart the rewrite operation, that is, immediately return to the head of the loop. But be careful not to create a dead loop!

  7. ' Chain| C ' (link to next rule chained)

    This tag causes the current rule to be linked to the next rule (which itself can be linked to its successor rule and can be so repeated). It produces such an effect: if a rule is matched, it will usually continue to process its successor, that is, the tag does not work, and if the rule cannot be matched, then its subsequent linked rules are ignored. For example, when performing an external redirect, for a directory-level rule set, you may need to delete ". www" (where ". www" should not appear).

  8. ' Type| T=mime-type ' (force MIME type types)

    The mandatory MIME type for the target file is Mime-type. For example, it can be used to simulate the Scriptalias directive in Mod_alias to internally force the MIME type of all files in the mapped directory to be "application/x-httpd-cgi".

  9. ' Nosubreq| NS ' (only for no internal sub-request processing no internal sub-request)

    This token forces the rewrite engine to skip the rewrite rule when the current request is an internal child request. For example, when Mod_include tries to search for possible directory default files (, Apache generates child requests internally. A child request, which is not necessarily useful, and may even throw an error if the entire ruleset is working. Therefore, you can use this tag to exclude certain rules. Follow these guidelines according to your needs: If you use URL prefixes with CGI scripts to force them to be handled by CGI scripts, the error rate (or overhead) of processing a child request is high, in which case you can use this tag.

  10. ' Nocase| NC ' (ignoring case of uppercase and lowercase)

    It makes the pattern ignore case, that is, ' A-Z ' and ' A-Z ' are not different when pattern matches the current URL.

  11. ' Qsappend| QSA ' (Append request string query string append)

    This flag forces the rewrite engine to append a request string to an existing replacement string, rather than a simple replacement. You can use this tag if you need to add information to the request string through a rewrite rule.

  12. ' Noescape| NE ' (no URI escaped in output no URI escaping)

    This flag prevents Mod_rewrite from applying a general URI escape rule to the overridden result. In general, special characters (such as '% ', ' $ ', '; ') And so on) will be escaped to the equivalent hexadecimal encoding. This tag prevents such escapes from allowing symbols such as percent sign to appear in the output, such as: rewriterule/foo/(. *)/bar?arg=p1\%3d$1 [R,ne]
    Can make '/foo/zed ' turn to a secure request '/bar?arg=p1=zed '.

  13. ' Passthrough| PT ' (handed over to the next processor pass through)

    This flag forces the rewrite engine to set the URI field in the internal structure Request_rec to the value of the FileName field, which is only a small modification to the Alias,scriptalias from other URIs to the file name translator Redirect The output of the command is subsequently processed. Give an example of what it means: if you want to rewrite/ABC as/def through the mod_rewrite rewrite engine, and then turn/def into/ghi by Mod_alias, you can: Rewriterule ^/abc (. *)/def$1 [PT]
    If the PT tag is omitted, although mod_rewrite works fine, that is, as a URI to the file name translator using the API, it can rewrite uri=/abc/... For filename=/def/..., however, subsequent mod_alias are invalidated when attempting to translate the URI to the file name.
    Note: You must use this tag if you need to mix a different module that contains a URI to the file name translator. Mixed use of Mod_alias and mod_rewrite is a typical example.

    For Apache hackers
    If the current Apache API in addition to the URI to the file name hook, there is a file name to the file name of the hook, you do not need this tag! However, if there is no such a hook, then this tag is the only solution. Apache Group has discussed this issue and will add such a hook in Apache version 2.0.

  14. ' Skip| S=num ' (Skip the successor rule skip)

    This flag forces the rewrite engine to skip the NUM rules succeeding the current matching rule. It can implement a pseudo-If-then-else construct: The last rule is the then clause, and the skip=n rule that is skipped is the ELSE clause. (IT and ' chain| The C ' tag is different!)

  15. ' Env| E=var:val ' (Set environment variable environment variable)

    This flag causes the value of the environment variable VAR to be Val, and Val can contain an extensible inverse reference to the regular expression $n and%n. This tag can be used multiple times to set multiple variables. These variables can be referenced indirectly in many subsequent cases, but usually in Xssi (via or CGI (e.g. $ENV {' VAR '}), or by%{env:var} in the pattern of subsequent rewritecond instructions. Use it to peel and remember some information from the URL.

  16. ' cookie|co=name:val:domain[:lifetime[:p Ath]] ' (set cookies)

    It sets a cookie on the client browser. The name of the cookie is "name" and its value is Val. The Domain field is the field of the cookie, such as '. ', the optional lifetime is the number of minutes of the cookie's lifetime, and the optional path is the cookie.

Further reading:

Detailed description of the. htaccess Rewrite rules

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.