Defining Rewriting Behavior

RewriteRule configures the rewriting of user requests. If a user request matches RewriteCond, the pattern in the user request is replaced with Substitution specified in the RewriteRule setting.

RewriteRule <Pattern> <Substitution> [flags]

Pattern Configuration

Patterns can use Perl compatible regular expressions, and you can use '!' to specify cases where the pattern does not match.

If '!' is used, the group pattern ($N) enclosed in parentheses cannot be used because unmatched patterns do not have values for group patterns ($N).

If RewriteRule is used alone, pattern matching uses URL-path. If RewriteRule is used with RewriteCond, the final matched pattern is used.

Substitution Configuration

Specifies what to substitute the matched URL with. The following values can be used.

Value Description

file-system path

When specifying the absolute path of the file system that starts with '/', use the file in the user response. However, the configured path must exist in the file-system.

URL-path

When a general URL path is used, use the appropriate resource.

Absolute URL

If an absolute URL is specified (for example, "http://<hostname>/file.html"), and the hostname matches the server, the scheme and hostname are stripped out and the resulting path is treated as a URL path. Otherwise, it is redirected to an external server.

-

It means no substitution should be performed.

$N (N=0..9)

Indicates the Nth group pattern among RewriteRule’s patterns.

%N (N=1..9)

Indicates the Nth group pattern among the last matched RewriteCond patterns.

%{VARNAME}

Refers to VARNAME provided by the server. The items applied to TestString of RewriteCond can be used.

?

The Query string is not changed. In order to change the string, add '?' to the substitute string. In order to delete a query string, place a '?' at the end of the substitute string.

'${mapname:key|default}' cannot be used because WebtoB does not support RewriteMap provided by Apache’s mod_rewrite.

Flags Configuration

The detailed behavior of RewriteRule can be configured through the settings in [flags].

Multiple plags can be set using commas (,), and the following values can be used:

  • B

    By default, backreferences used in Substitution ($N or %N) remove the %-encoding of the URL. However, if the [B] option is used, the URL’s %-encoding can be preserved.

    For example, if set in the following, '/C%2b%2b' is mapped to 'index.php?show=/C++' by default. However, if the [B] option is used, it will be mapped to 'index.php?show=/C%2b%2b'.

    RewriteRule ^(.*)$ index.php?show=$1
  • chain|C

    If the current rule does not match, the next rule will not be checked. If the next rule also uses the [C] option, it is also skipped.

  • cookie|CO=NAME:VAL:domain[:lifetime[:path[:secure[:httponly]]]]

    Add the Set-Cookie Header to a response to add a Cookie to a user’s browser.

  • discardpathinfo|DPI

    In per-directory context, RewriteRule distinguishes URI and PATH_INFO and combines them each time the rule is applied.

    [DPI] options apply RewriteRule without distinguishing PATH_INFO.

  • env|E=VAR:VAL

    Adds VAR=VAL to an environment variable. In VAL, regexp backreferences ($N or %N) can be used. This environment variable can be used in SSI or CGI, and can be used as %{ENV:VAR} among RewriteCond patterns.

  • forbidden|F

    Sends a "403 Forbidden" response.

  • gone|G

    Sends a "410 Gone" response.

  • handle|H=Content-handler

    Configures a content-handler.

  • last|L

    Signifies the end of the rewriting process. This option is similar to the break command in C.

  • next|N

    Executes the rewriting process again from the start to the changed URL. This option is similar to the continue command in C.

    Be careful when using this option, as it may cause an infinite loop.

  • nocase|NC

    Turns off case-sensitivity in patterns. For example, 'A' and 'a' are considered the same.

  • noescape|NE

    Does not use %-encoding for URL in the rewriting process.

  • nosubreq|NS

    Stops rewriting in the case of an internal request.

  • passthrough|PT

    Uses the result of rewriting from another handler.

  • qsappend|QSA

    When rewriting a query string, appends to the existing string instead of overwriting it.

  • redirect|R[=code]

    When the substitute string is an absolute URL, the URL is forcibly redirected even when the hostname matches a server’s host.

    If the code is not specified, 302 Moved Temporarily is used. In the code, the status code can be entered directly or temp (default), permanent, and seeother can be specified.

  • skip|S=num

    Skips the next 'num' rules. num is a numeric value.

  • type|T=MIME-type

    Specifies the content-type of the response.