Class RewriteRule

java.lang.Object
org.apache.catalina.valves.rewrite.RewriteRule

public class RewriteRule extends Object
Represents a rewrite rule for URL rewriting.
  • Field Details

    • conditions

      protected RewriteCond[] conditions
      The conditions associated with this rule.
    • pattern

      protected ThreadLocal<Pattern> pattern
      The compiled pattern for matching.
    • substitution

      protected Substitution substitution
      The substitution to apply.
    • patternString

      protected String patternString
      The pattern string.
    • substitutionString

      protected String substitutionString
      The substitution string.
    • flagsString

      protected String flagsString
      The flags string.
    • positive

      protected boolean positive
      Whether the pattern is positive (true) or negated (false).
    • chain

      protected boolean chain
      This flag chains the current rule with the next rule (which itself can be chained with the following rule, etc.). This has the following effect: if a rule matches, then processing continues as usual, i.e., the flag has no effect. If the rule does not match, then all following chained rules are skipped. For instance, use it to remove the ".www" part inside a per-directory rule set when you let an external redirect happen (where the ".www" part should not to occur!).
    • cookieName

      protected String cookieName
      The cookie name.
    • cookieValue

      protected String cookieValue
      The cookie value.
    • cookieDomain

      protected String cookieDomain
      The cookie domain.
    • cookieLifetime

      protected int cookieLifetime
      The cookie lifetime.
    • cookiePath

      protected String cookiePath
      The cookie path.
    • cookieSecure

      protected boolean cookieSecure
      Whether the cookie is secure.
    • cookieHttpOnly

      protected boolean cookieHttpOnly
      Whether the cookie is HTTP only.
    • cookieSubstitution

      protected Substitution cookieSubstitution
      The cookie substitution.
    • cookieResult

      protected ThreadLocal<String> cookieResult
      The cookie result.
    • env

      protected boolean env
      This forces a request attribute named VAR to be set to the value VAL, where VAL can contain regexp back references $N and %N which will be expanded. Multiple env flags are allowed.
    • envName

      protected ArrayList<String> envName
      The environment variable names.
    • envValue

      protected ArrayList<String> envValue
      The environment variable values.
    • envSubstitution

      protected ArrayList<Substitution> envSubstitution
      The environment substitutions.
    • envResult

      protected ArrayList<ThreadLocal<String>> envResult
      The environment results.
    • forbidden

      protected boolean forbidden
      This forces the current URL to be forbidden, i.e., it immediately sends back an HTTP response of 403 (FORBIDDEN). Use this flag in conjunction with appropriate RewriteConds to conditionally block some URLs.
    • gone

      protected boolean gone
      This forces the current URL to be gone, i.e., it immediately sends back an HTTP response of 410 (GONE). Use this flag to mark pages which no longer exist as gone.
    • host

      protected boolean host
      Host. This means this rule and its associated conditions will apply to host, allowing host rewriting (ex: redirecting internally *.foo.com to bar.foo.com).
    • last

      protected boolean last
      Stop the rewriting process here and don't apply any additional rewriting rules. This corresponds to the Perl last command or the break command from the C language. Use this flag to prevent the currently rewritten URL from being rewritten further by following rules. For example, use it to rewrite the root-path URL ('/') to a real one, e.g., '/e/www/'.
    • next

      protected boolean next
      Re-run the rewriting process (starting again with the first rewriting rule). Here the URL to match is again not the original URL but the URL from the last rewriting rule. This corresponds to the Perl next command or the continue command from the C language. Use this flag to restart the rewriting process, i.e., to immediately go to the top of the loop. But be careful not to create an infinite loop!
    • nocase

      protected boolean nocase
      This makes the Pattern case-insensitive, i.e., there is no difference between 'A-Z' and 'a-z' when Pattern is matched against the current URL.
    • noescape

      protected boolean noescape
      This flag keeps mod_rewrite from applying the usual URI escaping rules to the result of a rewrite. Ordinarily, special characters (such as '%', '$', ';', and so on) will be escaped into their hexcode equivalents ('%25', '%24', and '%3B', respectively); this flag prevents this from being done. This allows percent symbols to appear in the output, as in RewriteRule /foo/(.*) /bar?arg=P1\%3d$1 [R,NE] which would turn '/foo/zed' into a safe request for '/bar?arg=P1=zed'.
    • nosubreq

      protected boolean nosubreq
      This flag forces the rewriting engine to skip a rewriting rule if the current request is an internal sub-request. For instance, sub-requests occur internally in Apache when mod_include tries to find out information about possible directory default files (index.xxx). On sub-requests it is not always useful and even sometimes causes a failure to if the complete set of rules are applied. Use this flag to exclude some rules. Use the following rule for your decision: whenever you prefix some URLs with CGI-scripts to force them to be processed by the CGI-script, the chance is high that you will run into problems (or even overhead) on sub-requests. In these cases, use this flag.
    • qsappend

      protected boolean qsappend
      This flag forces the rewriting engine to append a query string part in the substitution string to the existing one instead of replacing it. Use this when you want to add more data to the query string via a rewrite rule.
    • qsdiscard

      protected boolean qsdiscard
      When the requested URI contains a query string, and the target URI does not, the default behavior of RewriteRule is to copy that query string to the target URI. Using the [QSD] flag causes the query string to be discarded. Using [QSD] and [QSA] together will result in [QSD] taking precedence.
    • redirect

      protected boolean redirect
      Prefix Substitution with http://thishost[:thisport]/ (which makes the new URL a URI) to force an external redirection. If no code is given an HTTP response of 302 (FOUND, previously MOVED TEMPORARILY) is used. If you want to use other response codes in the range 300-399 just specify them as a number or use one of the following symbolic names: temp (default), permanent, seeother. Use it for rules which should canonicalize the URL and give it back to the client, e.g., translate "/~" into "/u/" or always append a slash to /u/user, etc. Note: When you use this flag, make sure that the substitution field is a valid URL! If not, you are redirecting to an invalid location! And remember that this flag itself only prefixes the URL with http://thishost[:thisport]/, rewriting continues. Usually you also want to stop and do the redirection immediately. To stop the rewriting you also have to provide the 'L' flag.
    • redirectCode

      protected int redirectCode
      The redirect HTTP status code.
    • skip

      protected int skip
      This flag forces the rewriting engine to skip the next num rules in sequence when the current rule matches. Use this to make pseudo if-then-else constructs: The last rule of the then-clause becomes skip=N where N is the number of rules in the else-clause. (This is not the same as the 'chain|C' flag!)
    • type

      protected boolean type
      Force the MIME-type of the target file to be MIME-type. For instance, this can be used to set up the content-type based on some conditions. For example, the following snippet allows .php files to be displayed by mod_php if they are called with the .phps extension: RewriteRule ^(.+\.php)s$ $1 [T=application/x-httpd-php-source]
    • typeValue

      protected String typeValue
      The MIME type value.
    • valveSkip

      protected boolean valveSkip
      Allows skipping the next valve in the Catalina pipeline.
  • Constructor Details

    • RewriteRule

      public RewriteRule()
      Default constructor.
  • Method Details

    • parse

      public void parse(Map<String, RewriteMap> maps)
      Parse the rule using the provided rewrite maps.
      Parameters:
      maps - the rewrite maps
    • addCondition

      public void addCondition(RewriteCond condition)
      Add a condition to this rule.
      Parameters:
      condition - the condition to add
    • evaluate

      public CharSequence evaluate(CharSequence url, Resolver resolver)
      Evaluate the rule based on the context
      Parameters:
      url - The char sequence
      resolver - Property resolver
      Returns:
      null if no rewrite took place
    • toString

      public String toString()
      String representation.
      Overrides:
      toString in class Object
    • isEscapeBackReferences

      public boolean isEscapeBackReferences()
      Get whether back references are escaped.
      Returns:
      true if back references are escaped
    • setEscapeBackReferences

      public void setEscapeBackReferences(boolean escapeBackReferences)
      Set whether back references are escaped.
      Parameters:
      escapeBackReferences - true to escape back references
    • isChain

      public boolean isChain()
      Get whether this rule is chained.
      Returns:
      true if chained
    • setChain

      public void setChain(boolean chain)
      Set whether this rule is chained.
      Parameters:
      chain - true to chain with the next rule
    • getConditions

      public RewriteCond[] getConditions()
      Get the conditions for this rule.
      Returns:
      the conditions array
    • setConditions

      public void setConditions(RewriteCond[] conditions)
      Set the conditions for this rule.
      Parameters:
      conditions - the conditions array
    • isCookie

      public boolean isCookie()
      Get whether the cookie flag is set.
      Returns:
      true if cookie flag is set
    • setCookie

      public void setCookie(boolean cookie)
      Set the cookie flag.
      Parameters:
      cookie - true to set the cookie flag
    • getCookieName

      public String getCookieName()
      Get the cookie name.
      Returns:
      the cookie name
    • setCookieName

      public void setCookieName(String cookieName)
      Set the cookie name.
      Parameters:
      cookieName - the cookie name
    • getCookieValue

      public String getCookieValue()
      Get the cookie value.
      Returns:
      the cookie value
    • setCookieValue

      public void setCookieValue(String cookieValue)
      Set the cookie value.
      Parameters:
      cookieValue - the cookie value
    • getCookieResult

      public String getCookieResult()
      Get the cookie result.
      Returns:
      the cookie result
    • isEnv

      public boolean isEnv()
      Get whether the env flag is set.
      Returns:
      true if env flag is set
    • getEnvSize

      public int getEnvSize()
      Get the number of environment variables.
      Returns:
      the number of environment variables
    • setEnv

      public void setEnv(boolean env)
      Set the env flag.
      Parameters:
      env - true to set the env flag
    • getEnvName

      public String getEnvName(int i)
      Get the name of an environment variable.
      Parameters:
      i - the index
      Returns:
      the environment variable name
    • addEnvName

      public void addEnvName(String envName)
      Add an environment variable name.
      Parameters:
      envName - the name to add
    • getEnvValue

      public String getEnvValue(int i)
      Get the value of an environment variable.
      Parameters:
      i - the index
      Returns:
      the environment variable value
    • addEnvValue

      public void addEnvValue(String envValue)
      Add an environment variable value.
      Parameters:
      envValue - the value to add
    • getEnvResult

      public String getEnvResult(int i)
      Get the result of an environment variable substitution.
      Parameters:
      i - the index
      Returns:
      the environment variable result
    • isForbidden

      public boolean isForbidden()
      Get whether the forbidden flag is set.
      Returns:
      true if forbidden flag is set
    • setForbidden

      public void setForbidden(boolean forbidden)
      Set the forbidden flag.
      Parameters:
      forbidden - true to set the forbidden flag
    • isGone

      public boolean isGone()
      Get whether the gone flag is set.
      Returns:
      true if gone flag is set
    • setGone

      public void setGone(boolean gone)
      Set the gone flag.
      Parameters:
      gone - true to set the gone flag
    • isLast

      public boolean isLast()
      Get whether the last flag is set.
      Returns:
      true if last flag is set
    • setLast

      public void setLast(boolean last)
      Set the last flag.
      Parameters:
      last - true to set the last flag
    • isNext

      public boolean isNext()
      Get whether the next flag is set.
      Returns:
      true if next flag is set
    • setNext

      public void setNext(boolean next)
      Set the next flag.
      Parameters:
      next - true to set the next flag
    • isNocase

      public boolean isNocase()
      Get whether the nocase flag is set.
      Returns:
      true if nocase flag is set
    • setNocase

      public void setNocase(boolean nocase)
      Set the nocase flag.
      Parameters:
      nocase - true to set the nocase flag
    • isNoescape

      public boolean isNoescape()
      Get whether the noescape flag is set.
      Returns:
      true if noescape flag is set
    • setNoescape

      public void setNoescape(boolean noescape)
      Set the noescape flag.
      Parameters:
      noescape - true to set the noescape flag
    • isNosubreq

      public boolean isNosubreq()
      Get whether the nosubreq flag is set.
      Returns:
      true if nosubreq flag is set
    • setNosubreq

      public void setNosubreq(boolean nosubreq)
      Set the nosubreq flag.
      Parameters:
      nosubreq - true to set the nosubreq flag
    • isQsappend

      public boolean isQsappend()
      Get whether the qsappend flag is set.
      Returns:
      true if qsappend flag is set
    • setQsappend

      public void setQsappend(boolean qsappend)
      Set the qsappend flag.
      Parameters:
      qsappend - true to set the qsappend flag
    • isQsdiscard

      public final boolean isQsdiscard()
      Get whether the qsdiscard flag is set.
      Returns:
      true if qsdiscard flag is set
    • setQsdiscard

      public final void setQsdiscard(boolean qsdiscard)
      Set the qsdiscard flag.
      Parameters:
      qsdiscard - true to set the qsdiscard flag
    • isRedirect

      public boolean isRedirect()
      Get whether the redirect flag is set.
      Returns:
      true if redirect flag is set
    • setRedirect

      public void setRedirect(boolean redirect)
      Set the redirect flag.
      Parameters:
      redirect - true to set the redirect flag
    • getRedirectCode

      public int getRedirectCode()
      Get the redirect HTTP status code.
      Returns:
      the redirect code
    • setRedirectCode

      public void setRedirectCode(int redirectCode)
      Set the redirect HTTP status code.
      Parameters:
      redirectCode - the redirect code
    • getSkip

      public int getSkip()
      Get the number of rules to skip.
      Returns:
      the skip count
    • setSkip

      public void setSkip(int skip)
      Set the number of rules to skip.
      Parameters:
      skip - the number of rules to skip
    • getSubstitution

      public Substitution getSubstitution()
      Get the substitution.
      Returns:
      the substitution
    • setSubstitution

      public void setSubstitution(Substitution substitution)
      Set the substitution.
      Parameters:
      substitution - the substitution
    • isType

      public boolean isType()
      Get whether the type flag is set.
      Returns:
      true if type flag is set
    • setType

      public void setType(boolean type)
      Set the type flag.
      Parameters:
      type - true to set the type flag
    • getTypeValue

      public String getTypeValue()
      Get the MIME type value.
      Returns:
      the MIME type value
    • setTypeValue

      public void setTypeValue(String typeValue)
      Set the MIME type value.
      Parameters:
      typeValue - the MIME type value
    • getPatternString

      public String getPatternString()
      Get the pattern string.
      Returns:
      the pattern string
    • setPatternString

      public void setPatternString(String patternString)
      Set the pattern string.
      Parameters:
      patternString - the pattern string
    • getSubstitutionString

      public String getSubstitutionString()
      Get the substitution string.
      Returns:
      the substitution string
    • setSubstitutionString

      public void setSubstitutionString(String substitutionString)
      Set the substitution string.
      Parameters:
      substitutionString - the substitution string
    • getFlagsString

      public final String getFlagsString()
      Get the flags string.
      Returns:
      the flags string
    • setFlagsString

      public final void setFlagsString(String flagsString)
      Set the flags string.
      Parameters:
      flagsString - the flags string
    • isHost

      public boolean isHost()
      Get whether the host flag is set.
      Returns:
      true if host flag is set
    • setHost

      public void setHost(boolean host)
      Set the host flag.
      Parameters:
      host - true to set the host flag
    • getCookieDomain

      public String getCookieDomain()
      Get the cookie domain.
      Returns:
      the cookie domain
    • setCookieDomain

      public void setCookieDomain(String cookieDomain)
      Set the cookie domain.
      Parameters:
      cookieDomain - the cookie domain
    • getCookieLifetime

      public int getCookieLifetime()
      Get the cookie lifetime.
      Returns:
      the cookie lifetime in minutes
    • setCookieLifetime

      public void setCookieLifetime(int cookieLifetime)
      Set the cookie lifetime.
      Parameters:
      cookieLifetime - the cookie lifetime in minutes
    • getCookiePath

      public String getCookiePath()
      Get the cookie path.
      Returns:
      the cookie path
    • setCookiePath

      public void setCookiePath(String cookiePath)
      Set the cookie path.
      Parameters:
      cookiePath - the cookie path
    • isCookieSecure

      public boolean isCookieSecure()
      Get whether the cookie secure flag is set.
      Returns:
      true if cookie secure flag is set
    • setCookieSecure

      public void setCookieSecure(boolean cookieSecure)
      Set the cookie secure flag.
      Parameters:
      cookieSecure - true to set the cookie secure flag
    • isCookieHttpOnly

      public boolean isCookieHttpOnly()
      Get whether the cookie HTTP only flag is set.
      Returns:
      true if cookie HTTP only flag is set
    • setCookieHttpOnly

      public void setCookieHttpOnly(boolean cookieHttpOnly)
      Set the cookie HTTP only flag.
      Parameters:
      cookieHttpOnly - true to set the cookie HTTP only flag
    • isValveSkip

      public boolean isValveSkip()
      Get whether the valve skip flag is set.
      Returns:
      true if valve skip flag is set
    • setValveSkip

      public void setValveSkip(boolean valveSkip)
      Set the valve skip flag.
      Parameters:
      valveSkip - true to set the valve skip flag