[Switch] Flag description in Apache rewrite

The general pattern of rewrite rules is the following syntax.
RewriteRule pattern target [Flag1,Flag2,Flag3]
RewriteRule rules can be followed by a flag (Flag), and multiple flags can be added. Multiple flags are connected with commas ",".
 
First explain a special target value: "-", if the target is "-", then the requested URL will not be modified.
 
The following is a detailed introduction to the meaning of each flag.
 
C|chain
C|chain means: The character 'C' or the string 'chain' indicates that there are other rewriting rules in addition to the rewriting rules of this line, which is equivalent to the ampersand '&' in ordinary programming languages, if the first rule If there is a match, the next condition is matched. If the first or middle one is unsuccessful, the following ones will be skipped.
 
CO | cookie
CO|cookie Meaning: The character 'CO' or the string 'cookie' means that when some special rules are matched, a cookie is allowed to be set, and the setting parameter contains 3 required fields and 2 optional fields.
The three required fields are the name, value, and domain name of the cookie. The other two optional fields are the lifetime and path of the cookie. The default lifetime of the cookie is the session time of the browser. The default The path is '/', for the entire site.
The actual use example is as follows
RewriteEngine On
# RewriteRule matching pattern - [CO=COOKIE name: COOKIE value: COOKIE domain name: lifetime: path] Colons are used for each parameter: connect
RewriteRule ^/index.html - [CO=mycookie:myCookieValue:.test.com:1440 :/]
#RewriteRule ^/index.html - [CO=mycookie:myCookieValue:.test.com] or omit the following parameters.
The above rule means to set a COOKIE value when requesting the index.html file. The COOKIE name is mycookie, the value is: myCookieValue, the effective domain name is .test.com, and the effective time is calculated in minutes. That is, the survival time is 1 day = 24 hours = 1440 minutes.
E|env
E|env means: The character 'E' or the string 'env' means you can set an environment variable. Note that the variable takes effect after this rule runs.
Look at a simple example, that is, apache does not record the reading record of the picture when recording the log. Then the following rules are useful.
RewriteRule \.(png|gif|jpg) - [E=image:1]
CustomLog logs/access_log combined env=!image
 
F|forbidden
F|forbidden means: The character 'F' or the string 'forbidden' means access is forbidden. The Apache server will return a 403 forbidden access status code to the client.
The following rules indicate that obtaining or downloading exe program files is displayed as forbidden access.
RewriteRule \.exe - [F]
 
G|gone
G|gone means: The character 'G' or the string 'gone' indicates that the server response status code is: 410 Usually 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. It doesn't care about case.
RewriteRule oldproduct - [G,NC]
 
H|handler
H|handler means: The character 'H' or the string 'handler' indicates that a certain type of handler is used to process the requested resource. For example, when requesting some files without a suffix. The following columns indicate that when the requested URL does not contain a '.', force the use of PHP to handle such requests.
RewriteRule !\. - [H=application/x-httpd-php]
 
L|last
L|last Meaning: The character 'L' or the string 'last' indicates that the current rule is the last rule, and stops the analysis of subsequent rule rewriting. This flag is used very frequently.
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^(.*) index.php?req=$1 [L]
The place to pay attention, when using the [L] flag, be sure to pay attention to your matching conditions, it will not easily make your rewrite rules fall into an infinite loop, for example, you need to define that all page requests on a page are rewritten to an index .php file, then you must pay attention to the matching conditions to ensure that the rewriting rules are executed only when the requested script is not index.php. Otherwise, it is obvious that the current page request is index.php, of course, this request is rewritten to index.php Then index.php is rewritten to index.php.. This is repeated. The page will report an error. The error log will report that you exceeded the maximum number of redirects.
 
N|next
N|next 意思: 字符 'N' 或者 字符串 'next' 表示重新回到规则顶部重复执行. 一般在极端情况下用这个标志. 相当于一个while循环, 知道匹配失败时返回. 下面的例子表示把请求地址中的所有A字符替换成B字符.
RewriteRule (.*)A(.*) $1B$2 [N]
 
NC|nocase
NC|nocase 意思: 字符 'NC' 或者 字符串 'nocase' 表示请求的规则部分不区分大小写. 类似正则式里的/xxx/i 模式.
RewriteRule (.*\.(jpg|gif|png))$ http://images.test.com$1 [P,NC]
 
NE|noescape
NE|noescape 意思: 字符 'NE' 或者 字符串 'noescape' 表示不对URL中的特殊字符进行 hexcode 转码.看下面的例子:
RewriteRule ^/share/(.+) /goShare.html#$1 [NE,R]
上面的例子表示所有请求 /share/xxx.xx的请求都会被定向到/goShare.html文件上. 并且后面的部分作为一个#后面的值. 如果不加NE标志的话, #将被转义成 %23 这样就造成 404 错误了.
 
NS|nosubreq
NS|nosubreq 意思: 字符 'NS' 或者 字符串 'nosubreq' 表示只用于不是内部子请求.比如,在mod_include试图搜索可能的目录默认文件(index.xxx)时, Apache会内部地产生子请求。对子请求,它不一定有用的,而且如果整个规则集都起作用,它甚至可能会引发错误。所以,可以用这个标记来排除某些规则。 根据你的需要遵循以下原则: 如果你使用了有CGI脚本的URL前缀,以强制它们由CGI脚本处理,而对子请求处理的出错率(或者开销)很高,在这种情况下,可以使用这个标记。
 
P|proxy
P|proxy 意思: 字符 'P' 或者 字符串 'proxy' 标志需要模块 mod_proxy 支持, 类似一个分发器网关的作用.比如网站的所有图片想用单独的一台服务器来运行. 那么先前的代码里的图片请求的时候, 直接定向到图片服务器去.
RewriteRule (.*)\.(jpg|gif|png) http://images.example.com$1.$2 [P]
使用[P]标志, 意味着使用了[L]标志, 因为使用该标志后马上就重定向到新地址了. 后面的重写规则会被忽略掉.
 
PT|passthrough
PT|passthrough 意思: 字符 'PT' 或者 字符串 'passthrough' 表示替换URL请问部分的地址.看例子
Alias /icons /usr/local/apache/icons
RewriteRule /pics/(.+)\.jpg /icons/$1.gif [PT]
当请求/pics/下的图片文件时, 实际是返回的是 /icons/目录下的同名文件. 需要注意的是一定要设置 [PT] 标志. 否则Alias设置无效.
 
QSA|qsappend
QSA|qsappend 意思: 字符 'QSA' 或者 字符串 'qsappend' 不怎么好表示. 看例子:
RewriteRule /pages/(.+) /page.php?page=$1 [QSA]
如果又标志: [QSA] 那么重写后的URL是: /page.php?page=123&one=two
如果没有[QSA]标志, 那么结果是: /page.php?page=123
此标记强制重写引擎在已有的替换串中追加一个请求串,而不是简单的替换。 如果需要通过重写规则在请求串中增加信息,就可以使用这个标记。
 
R|redirect
R|redirect 意思: 字符 'R' 或者 字符串 'redirect' 表示进行重定向, 状态码在300-399里随机出, 默认是 302 重定向.通常和标志L一起使用. 使用模式: [R[=302]]
 
S|skip
T|type 意思: 字符 'S' 或者 字符串 'skip' 表示跳过执行下面的几个重写规则. 又点类似goto. 看下面的例子, 如果URL请求的文件不存在的话就跳过下面的两行重写规则.
# 请求的文件是否存在
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# 不存在的情况
RewriteRule .? - [S=2]
RewriteRule (.*\.gif) images.php?$1
RewriteRule (.*\.html) docs.php?$1
 
T|type
T|type 意思: 字符 'T' 或者 字符串 'type' 表示为apache设置特定请求的响应类型. 也就是常说的 MIME type,比如一个perl脚本. 希望给客户端显示文本源码, 那么可以这样做:
RewriteRule \.pl$ - [T=text/plain]
或者你的服务器上的文件没有设置扩展名. 那么可以通知重写添加该文件的类型. 方便客户端显示.
RewriteRule IMG - [T=image/jpg]
 
以上就是Apache 官方文档提到的全部标志值及其相关的意思了. 接下来我们将介绍他们用到具体的实际例子中去. 上面代码的例子看不懂没关系. 接下来我会很详细的解释它的. :)  这篇就先到这里吧.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325644062&siteId=291194637