Converting mod_rewrite directives

Overview

The URL Rewriting engine in Abyss Web Server is comparable to the mod_rewrite module in Apache. Both offer similar features but they are not fully equivalent.

This article explains how to convert mod_rewrite directives to URL Rewriting parameters in Abyss Web Server. In most cases, the conversion straightforward. But in some rare cases, and for some of the most obscure features of mod_rewrite, conversion is possible provided that you change or rewrite the rules.

If you are using Abyss Web Server version 2.7 or prior, please refer to the older version of this article.

mod_rewrite directives syntax

mod_rewrite directives are put in .htaccess files (in subdirectories) or in the main configuration file httpd.conf.

The following directives are related to mod_rewrite and will be used for conversions:

RewriteBase
RewriteCond
RewriteRule

A typical configuration of mod_rewrite looks as follows:

# Some comments
RewriteBase PATH

RewriteCond %{VARIABLE1CONDITION1
RewriteCond %{VARIABLE2CONDITION2
RewriteRule REGEX REPLACEMENT [FLAGS]

RewriteBase is optional. By default, the base path is the current location of the directory where the .htaccess file is. Each time a RewriteRule is found, the base path is updated with the parameter following the directive. The base path is useful when processing RewriteRule parameters.

RewriteCond is also optional. 0 or more RewriteCond directives can precede a given RewriteRule declaration.

RewriteRule is mandatory and ends the declaration of a rule. More sequences of RewriteBase/RewriteCond/RewriteRule could follow to define other rules.

Lines which start with # are comments and should be ignored. Lines referencing other directives are also to be ignored during the conversion as they are not related to mod_rewrite.

Starting the conversion

For each RewriteRule directive, you should create a URL Rewriting rule.

  • Locate the RewriteRule you'll convert.
  • Locate all the RewriteCond directives that precede it directly (there are 0 or more of them).
  • Locate the last RewriteBase directive preceding the RewriteRule you'll convert. You may not find a RewriteBase in some cases.
  • If you've found a RewriteBase directive, the base path of the current rule will be the path that is referenced in that directive. Otherwise, the base path is the virtual path of the directory where the .htaccess file you're converting is located. For example, if there is no RewriteBase, if the .htaccess file you're converting is inside C:\sites\firstsite\forum, and if your Documents Path is C:\sites\firstsite\, then the base path is /forum.
  • Now open Abyss Web Server console, press the Configure associated with the host you'll add the URL rewriting rules to, and select URL Rewriting.
  • Press Add in URL Rewriting rules table to create a new rule.

Conversion of the RewriteRule directive

  • The RewriteRule directive has the following syntax:

    RewriteRule REGEX REPLACEMENT [FLAGS]

    [FLAGS] is optional an may not be always present.
    REGEX is a regular expression.
  • In the Abyss Web Server console, set Type to Relative to base.
  • Virtual Path Regular Expression the regular expression REGEX used in RewriteRule.
  • Enter in Redirect to the REPLACEMENT string used in RewriteRule.
  • If the RewriteRule directive has flags, convert each one of them as explained below:
    chain/C (chained with next rule)
    From Apache manual: "This flag chains the current rule with the next rule (which itself can be chained with the following rule, and so on). This has the following effect: if a rule matches, then processing continues as usual - the flag has no effect. If the rule does not match, then all following chained rules are skipped." No direct conversion is possible unless you reorder your URL Rewriting rules and correctly set the Next Action in Abyss Web Server for each rule.
    cookie/CO=NAME:VAL:domain[:lifetime[:path]] (set cookie)
    No equivalent in Abyss Web Server.
    env/E=VAR:VAL (set environment variable)
    No equivalent in Abyss Web Server.
    forbidden/F (force URL to be forbidden)
    Set If this rule matches to Report an error to the client and set Status Code to 403 - Forbidden.
    gone/G (force URL to be gone)
    Set If this rule matches to Report an error to the client and set Status Code to 410 - Gone.
    last/L (last rule)
    Set Next Action to Stop matching
    next/N (next round)
    Set Next Action to Stop matching.
    nocase/NC (no case)
    Uncheck Case Sensitive
    noescape/NE (no URI escaping of output)
    Uncheck Escape Redirection Location.
    nosubreq/NS (not for internal sub-requests)
    Uncheck Apply to subrequests too.
    proxy/P (force proxy)
    No equivalent in Abyss Web Server.
    passthrough/PT (pass through to next handler)
    No equivalent in Abyss Web Server.
    qsappend/QSA (query string append)
    Check Append Query String.
    redirect/R[=code] (force redirect)
    Set If this rule matches to Perform an external redirection and set Status Code to the value of code if available or to 302.
    skip/S=num (skip next rule(s))
    From Apache manual: "This flag forces the rewriting engine to skip the next num rules in sequence, if the current rule matches." No direct conversion is possible unless you reorder your URL Rewriting rules and correctly set the Next Action in Abyss Web Server for each rule.
    type/T=MIME-type (force MIME type)
    No equivalent in Abyss Web Server.

Conversion of the RewriteCond directives

Now it's time to convert the RewriteCond directives associated with the RewriteRule we're working on. Remember that only the RewriteCond immediately preceding the RewriteRule are to be taken into account. If there are no RewriteCond directives, conversion is over.

  • A RewriteCond directive has the form:

    RewriteCond %{VARIABLECOND [FLAGS]

    If the first argument of the RewriteCond you're converting contains a string which is not conforming to the syntax %{VARIABLE}, it will be impossible to convert the mod_rewrite rule to an Abyss Web Server URL Rewriting rule.
    [FLAGS] are optional and may not be always present.
    For each RewriteCond, press Add in the Conditions table and enter the value of VARIABLE in the Variable field.
  • If COND is a regular expression preceded by !, select Does not match with in Operator. If it is a regular expression not preceded by !, set Operator to Matches with. Next enter the regular expression in Regular Expression field.
  • Otherwise, COND is one of the following tests:
    <VALUE (lexicographically precedes)
    Set Operator to < and enter VALUE in the field Value.
    !<VALUE
    Set Operator to >= and enter VALUE in the field Value.
    >VALUE (lexicographically follows)
    Set Operator to > and enter VALUE in the field Value.
    !>VALUE
    Set Operator to <= and enter VALUE in the field Value.
    =VALUE (lexicographically equal)
    Set Operator to = and enter VALUE in the field Value.
    !=VALUE
    Set Operator to Is different from and enter VALUE in the field Value.
    -d (is directory)
    Set Operator to Is a directory.
    !-d
    Set Operator to Is not a directory.
    -f (is regular file)
    Set Operator to Is a file.
    !-f
    Set Operator to Is not a file.
    -s (is regular file, with size)
    Set Operator to Exists and is not an empty file.
    !-s
    Set Operator to Does not exist and is an empty file.
    -l/!-l (is/isn't symbolic link)
    No equivalent in Abyss Web Server.
    -F/!-F (is/isn't existing file, via subrequest)
    No equivalent in Abyss Web Server.
    -U/!-U (is/isn't existing URL, via subrequest)
    No equivalent in Abyss Web Server.
  • If FLAGS are present, their conversion should be done as follows:
    nocase/NC (no case)
    Uncheck Case Sensitive.
    ornext/OR (or next condition)
    The only case where a direct conversion is possible is when you have two or more consecutive RewriteCond operating on the same variable. In such a case, the regular expressions of each condition have to be concatenated with a | sign. For example:

    RewriteCond %{HTTP_USER_AGENT} Mozilla [OR]
    RewriteCond %{HTTP_USER_AGENT} Opera [OR]
    RewriteCond %{HTTP_USER_AGENT} Lynx

    could be combined in a single RewriteCond:

    RewriteCond %{HTTP_USER_AGENT} Mozilla|Opera|Lynx

    and thus it suffices to have a single condition on variable HTTP_USER_AGENT which checks if its value matches with Mozilla|Opera|Lynx.

See also