Address: http://hi.baidu.com/b13785022083/blog/item/5ff315fb9d794dd3b58f3179.html
The general mode of rewrite rules is the following syntax. rewriterule pattern target [flag1, flag2, flag3] The rewriterule rule can be followed by a flag or multiple flags. Multiple flags are separated by commas. first, specify a special target value: "-". If the target is "-", the requested URL will not be modified. the following describes in detail the meaning of each flag.
C | chainC | chain meaning: The character 'C' or the string 'chain' indicates that there are other Rewrite Rules in addition to the row rewrite rules, which is equivalent to the common program language and symbol '&', if the first rule matches, the next condition is matched. if the first or intermediate match fails. all the later parts will be skipped.
CO | cookieCO | cookie meaning: The character 'co' or string 'cookies' indicates that when some special rules are matched, a cookie can be set, the Setting Parameter contains three required fields and two optional fields. the three required fields are the cookie name, value, and domain name to which the cookie belongs. The other two optional fields are the cookie survival time and path. the default cookie survival time is the browser session time. the default path is '/' for the entire website. the actual example is as follows: rewriteengine on
# Rewriterule matching mode-[CO = cookie name: cookie value: Cookie Domain Name: survival time: path] each parameter is connected by a colon
Rewriterule ^/index.html-[CO = mycookie: mycookievalue: .test.com: 1440:/]
# Rewriterule ^/index.html-[CO = mycookie: mycookievalue: .test.com] or omit the following parameters. the preceding rule indicates setting a cookie value when requesting the index.html file. the cookie name is mycookie. The value is mycookievalue and the Effective domain name is .test.com. The effective time is calculated in minutes. that is, the survival time is 1 day = 24 hours = 1440 minutes.
E | envE | env: 'E' or 'env' indicates that you can set an environment variable. note that the variable takes effect after the rule is run. A simple example is that Apache does not record the reading records of images when logging. the following rules are useful. rewriterule \. (PNG | GIF | JPG)-[E = image: 1]
Customlog logs/access_log combined Env =! Image
F | forbiddenF | forbidden: The character 'f' or the string 'forbidden 'indicates that access is prohibited. the Apache server returns the 403 forbidden access status code to the client. the following rules indicate that access is forbidden to obtain or download EXE files. rewriterule \. exe-[F]
G | goneG | gone indicates that the server response status code is: 410 when this flag is used, the target value is set to "-" and the requested resource is valid. the following example indicates that the old resource is valid. and case insensitive.
Rewriterule oldproduct-[g, NC]
H | HandlerH | handler: 'H' or the string 'handler' indicates to force a type of handler to process the requested resource. for example, when you request a file without a suffix. the following column indicates that when the request URL does not contain '. ', force PHP to process such requests. rewriterule! \.-[H = application/X-httpd-PHP]
L | lastL | last indicates that the current rule is the last rule, and the rule is overwritten after the analysis is stopped. This flag is frequently used. rewritecond % {request_uri }! Index \. php
Rewriterule ^ (. *) index. php? Req = $1 [l]. When using the [l] Mark, pay attention to your matching conditions, it is not very easy to put your rewrite rules into an endless loop. For example, you need to define that all page requests on the page are overwritten to an index. PHP file, you must make sure that the requested script is not the index when matching the condition. rewrite Rules are executed only when PHP is used. otherwise it is obvious that the current page is requesting index. PHP, of course, this request is overwritten to index. PHP then index. PHP is rewritten to index again. PHP .. this operation is executed repeatedly. an error is reported on the page. the error log records the maximum number of redirection times you have exceeded.
N | nextN | next indicates that the character 'n' or the string 'Next' indicates repeated execution at the top of the rule. this sign is generally used in extreme cases. it is equivalent to a while loop. If the matching fails, it is returned. the following example replaces all a characters in the request address with B characters. rewriterule (. *) (. *) $ 1b $2 [N]
NC | nocaseNC | nocase: The character 'nc 'or the string 'nocase' indicates that the request rule is case insensitive. it is similar to the/XXX/I mode in the regular expression. rewriterule (. *\. (JPG | GIF | PNG) $ http://images.test.com $1 [p, NC]
Ne | noescapeNe | noescape: The character 'ne 'or the string 'noescape' indicates that hexcode transcoding is not performed for special characters in the URL. see the following example: rewriterule ^/share /(. +)/gow..html #$1 [NE, R] the preceding example indicates all requests/share/xxx. XX requests are directed to the/gow.html file. and the following part is used as a # value. if the ne flag is not added, # will be converted to % 23, resulting in 404 errors.
NS | nosubreqNS | nosubreq indicates that the character 'ns' or the string 'nosubreq' indicates that it is only used for internal subrequests. for example, when mod_include tries to search for a possible default directory file (index. XXX), Apache will request internally. It is not necessarily useful for subrequests. If the entire rule set works, it may even cause errors. Therefore, you can use this tag to exclude certain rules. Follow the following principles as needed: if you use a URL prefix with CGI scripts to force them to be processed by CGI scripts, the error rate (or overhead) of sub-requests is high, in this case, you can use this tag.
P | proxyP | Proxy: the 'p' or string 'proxy' sign must be supported by the module mod_proxy, similar to the function of a distributor gateway. for example, all images of a website can be run on a single server. the image request in the previous code is directly directed to the image server. rewriterule (. *)\. (JPG | GIF | PNG) http://images.example.com $1. $2 [p] use the [p] flag, which means that the [l] flag is used, because the flag will be redirected to the new address immediately after it is used. the subsequent Rewrite Rules will be ignored.
Pt | passthroughPt | passthrough: The character 'pt' or the string 'passthrough 'indicates the address used to replace the URL. See the example alias/icons/usr/local/Apache/icons.
Rewriterule/pics /(. + )\. JPG/icons/$1.gif [pt] When you request an image file under/pics/, it actually returns a file of the same name under the/icons/directory. note that the [pt] flag must be set. otherwise, the alias settings are invalid.
QSA | qsappendQSA | qsappend: The character 'qsa 'or the string 'qsappend' is not very good. See the example: rewriterule/pages/(. +)/page. php? Page = $1 [QSA] If the flag is [QSA], the URL after rewriting is:/page. php? Page = 123 & one = two without the [QSA] flag, the result is:/page. php? Page = 123 this flag forces the rewrite engine to append a request string to an existing replacement string, instead of simply replacing it. If you need to add information to the request string by using the rewrite rule, you can use this flag.
R | redirectR | redirect: The character 'R' or string 'redirect' indicates redirection. the status code is randomly generated in 302-. The default value is redirection. it is usually used with Flag L. usage mode: [R [= 302]
S | skipT | type indicates that the following rewrite rules are skipped when the character's or string 'skip' is used. similar to goto. let's look at the example below. If the URL requested file does not exist, we will skip the following two rewrite rules. # Whether the requested file exists
Rewritecond % {request_filename }! -F
Rewritecond % {request_filename }! -D
# Nonexistent
Rewriterule .? -[S = 2]
Rewriterule (. * \. GIF) images. php? $1
Rewriterule (. * \. html) docs. php? $1
T | TypeT | type indicates that the response type of a specific request is set for Apache. that is, MIME type, such as a Perl script. to display the text source code to the client, you can do this: rewriterule \. PL $-[T = text/plain] or the file on your server does not have an extension set. the file type can be added to the rewrite notification. ease of client display. rewriterule IMG-[T = image/JPG] and above are all the mark values mentioned in the Apache official document and their meanings. next we will introduce the actual examples they use. it does not matter if the above code example does not understand. next I will explain it in detail. :) this article is here first.