mod_headers

最後更新: 2021-09-28

介紹

control and modify HTTP request and response headers

目錄

  • Header
  • RequestHeader

Usage

 

Syntax:

Header [condition] ACTION header [value] [replacement] [early|env=[!]varname|expr=expression]]

ACTION: set|append|merge|add|unset|edit

unset

The request header of this name is removed, if it exists.

If there are multiple headers of the same name, all will be removed. value must be omitted.

set

The request header is set, replacing any previous header with this name

 * 比 add 好, 不會導致重復 header 出現

add

The request header is added to the existing set of headers, even if this header already exists.

append

The request header is appended to any existing header of the same name.

When a new value is merged onto an existing header it is separated from the existing header with a comma.

This is the HTTP standard way of giving a header multiple values.

Header append Cache-Control no-cache
Header append Cache-Control no-cache
Header append Cache-Control no-store

結果

Cache-Control: no-cache, no-cache, no-store

merge

The request header is appended to any existing header of the same name,

unless the value to be appended already appears in the existing header's comma-delimited list of values.

Header merge Cache-Control no-cache
Header merge Cache-Control no-cache
Header merge Cache-Control no-store

結果

Cache-Control: no-cache, no-store

edit

# replacing https: with http: in the Destination header:
# mod_headers can be applied either early or late in the request.
# The normal mode is late
# early directives are processed before the request path's configuration
# will fail in contexts such as <Directory> or <Location>.

RequestHeader edit Destination    ^https:     http:    early

Example

<ifModule mod_headers.c>
    Header unset ETag
    Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>

 

[early|env=[!]varname|expr=expression]]

early

Specifies early processing. The normal mode is late.

Early mode is designed as a test/debugging aid for developers.

功能

Directives defined using the early keyword are set right at the beginning of processing the request.

This means they can be used to simulate different requests and set up test cases,

but it also means that headers may be changed at any time by other modules before generating a Response.

env=[!]varname

The directive is applied if and only if the environment variable varname exists.

ie.

Header always set Access-Control-Allow-Origin %{origin_is}e env=origin_is

 

 


Condition

 

The optional condition argument determines which internal table of responses headers
this directive will operate against: onsuccess (default, can be omitted) or always.

always

Header are added to the response even on error (non-success (non-2xx)),
and persisted across internal redirects (i.e. ErrorDocument handlers).

 * mod_proxy_fcgi uses the always table to store headers

情況:

PHP proxy setup with mod_proxy_fcgi

backend PHP scripts adds the "X-Foo: bar" header to each HTTP response

# 如果不 unset, the header duplicated with both values

Header onsuccess unset X-Foo
Header always set X-Foo "baz"

 


RequestHeader

 

allowing incoming headers to be modified.

 

 

Creative Commons license icon Creative Commons license icon