Class RewriteRule
- java.lang.Object
-
- org.apache.catalina.valves.rewrite.RewriteRule
-
public class RewriteRule extends java.lang.ObjectRepresents a rewrite rule for URL rewriting.
-
-
Field Summary
Fields Modifier and Type Field Description protected booleanchainThis flag chains the current rule with the next rule (which itself can be chained with the following rule, etc.).protected RewriteCond[]conditionsThe conditions associated with this rule.protected booleancookieThis sets a cookie on the client's browser.protected java.lang.StringcookieDomainThe cookie domain.protected booleancookieHttpOnlyWhether the cookie is HTTP only.protected intcookieLifetimeThe cookie lifetime.protected java.lang.StringcookieNameThe cookie name.protected java.lang.StringcookiePathThe cookie path.protected java.lang.ThreadLocal<java.lang.String>cookieResultThe cookie result.protected booleancookieSecureWhether the cookie is secure.protected SubstitutioncookieSubstitutionThe cookie substitution.protected java.lang.StringcookieValueThe cookie value.protected booleanenvThis 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.protected java.util.ArrayList<java.lang.String>envNameThe environment variable names.protected java.util.ArrayList<java.lang.ThreadLocal<java.lang.String>>envResultThe environment results.protected java.util.ArrayList<Substitution>envSubstitutionThe environment substitutions.protected java.util.ArrayList<java.lang.String>envValueThe environment variable values.protected java.lang.StringflagsStringThe flags string.protected booleanforbiddenThis forces the current URL to be forbidden, i.e., it immediately sends back an HTTP response of 403 (FORBIDDEN).protected booleangoneThis forces the current URL to be gone, i.e., it immediately sends back an HTTP response of 410 (GONE).protected booleanhostHost.protected booleanlastStop the rewriting process here and don't apply any additional rewriting rules.protected booleannextRe-run the rewriting process (starting again with the first rewriting rule).protected booleannocaseThis 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.protected booleannoescapeThis flag keeps mod_rewrite from applying the usual URI escaping rules to the result of a rewrite.protected booleannosubreqThis flag forces the rewriting engine to skip a rewriting rule if the current request is an internal sub-request.protected java.lang.ThreadLocal<java.util.regex.Pattern>patternThe compiled pattern for matching.protected java.lang.StringpatternStringThe pattern string.protected booleanpositiveWhether the pattern is positive (true) or negated (false).protected booleanqsappendThis flag forces the rewriting engine to append a query string part in the substitution string to the existing one instead of replacing it.protected booleanqsdiscardWhen 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.protected booleanredirectPrefix Substitution with http://thishost[:thisport]/ (which makes the new URL a URI) to force an external redirection.protected intredirectCodeThe redirect HTTP status code.protected intskipThis flag forces the rewriting engine to skip the next num rules in sequence when the current rule matches.protected SubstitutionsubstitutionThe substitution to apply.protected java.lang.StringsubstitutionStringThe substitution string.protected booleantypeForce the MIME-type of the target file to be MIME-type.protected java.lang.StringtypeValueThe MIME type value.protected booleanvalveSkipAllows skipping the next valve in the Catalina pipeline.
-
Constructor Summary
Constructors Constructor Description RewriteRule()Default constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddCondition(RewriteCond condition)Add a condition to this rule.voidaddEnvName(java.lang.String envName)Add an environment variable name.voidaddEnvValue(java.lang.String envValue)Add an environment variable value.java.lang.CharSequenceevaluate(java.lang.CharSequence url, Resolver resolver)Evaluate the rule based on the contextRewriteCond[]getConditions()Get the conditions for this rule.java.lang.StringgetCookieDomain()Get the cookie domain.intgetCookieLifetime()Get the cookie lifetime.java.lang.StringgetCookieName()Get the cookie name.java.lang.StringgetCookiePath()Get the cookie path.java.lang.StringgetCookieResult()Get the cookie result.java.lang.StringgetCookieValue()Get the cookie value.java.lang.StringgetEnvName(int i)Get the name of an environment variable.java.lang.StringgetEnvResult(int i)Get the result of an environment variable substitution.intgetEnvSize()Get the number of environment variables.java.lang.StringgetEnvValue(int i)Get the value of an environment variable.java.lang.StringgetFlagsString()Get the flags string.java.lang.StringgetPatternString()Get the pattern string.intgetRedirectCode()Get the redirect HTTP status code.intgetSkip()Get the number of rules to skip.SubstitutiongetSubstitution()Get the substitution.java.lang.StringgetSubstitutionString()Get the substitution string.java.lang.StringgetTypeValue()Get the MIME type value.booleanisChain()Get whether this rule is chained.booleanisCookie()Get whether the cookie flag is set.booleanisCookieHttpOnly()Get whether the cookie HTTP only flag is set.booleanisCookieSecure()Get whether the cookie secure flag is set.booleanisEnv()Get whether the env flag is set.booleanisEscapeBackReferences()Get whether back references are escaped.booleanisForbidden()Get whether the forbidden flag is set.booleanisGone()Get whether the gone flag is set.booleanisHost()Get whether the host flag is set.booleanisLast()Get whether the last flag is set.booleanisNext()Get whether the next flag is set.booleanisNocase()Get whether the nocase flag is set.booleanisNoescape()Get whether the noescape flag is set.booleanisNosubreq()Get whether the nosubreq flag is set.booleanisQsappend()Get whether the qsappend flag is set.booleanisQsdiscard()Get whether the qsdiscard flag is set.booleanisRedirect()Get whether the redirect flag is set.booleanisType()Get whether the type flag is set.booleanisValveSkip()Get whether the valve skip flag is set.voidparse(java.util.Map<java.lang.String,RewriteMap> maps)Parse the rule using the provided rewrite maps.voidsetChain(boolean chain)Set whether this rule is chained.voidsetConditions(RewriteCond[] conditions)Set the conditions for this rule.voidsetCookie(boolean cookie)Set the cookie flag.voidsetCookieDomain(java.lang.String cookieDomain)Set the cookie domain.voidsetCookieHttpOnly(boolean cookieHttpOnly)Set the cookie HTTP only flag.voidsetCookieLifetime(int cookieLifetime)Set the cookie lifetime.voidsetCookieName(java.lang.String cookieName)Set the cookie name.voidsetCookiePath(java.lang.String cookiePath)Set the cookie path.voidsetCookieSecure(boolean cookieSecure)Set the cookie secure flag.voidsetCookieValue(java.lang.String cookieValue)Set the cookie value.voidsetEnv(boolean env)Set the env flag.voidsetEscapeBackReferences(boolean escapeBackReferences)Set whether back references are escaped.voidsetFlagsString(java.lang.String flagsString)Set the flags string.voidsetForbidden(boolean forbidden)Set the forbidden flag.voidsetGone(boolean gone)Set the gone flag.voidsetHost(boolean host)Set the host flag.voidsetLast(boolean last)Set the last flag.voidsetNext(boolean next)Set the next flag.voidsetNocase(boolean nocase)Set the nocase flag.voidsetNoescape(boolean noescape)Set the noescape flag.voidsetNosubreq(boolean nosubreq)Set the nosubreq flag.voidsetPatternString(java.lang.String patternString)Set the pattern string.voidsetQsappend(boolean qsappend)Set the qsappend flag.voidsetQsdiscard(boolean qsdiscard)Set the qsdiscard flag.voidsetRedirect(boolean redirect)Set the redirect flag.voidsetRedirectCode(int redirectCode)Set the redirect HTTP status code.voidsetSkip(int skip)Set the number of rules to skip.voidsetSubstitution(Substitution substitution)Set the substitution.voidsetSubstitutionString(java.lang.String substitutionString)Set the substitution string.voidsetType(boolean type)Set the type flag.voidsetTypeValue(java.lang.String typeValue)Set the MIME type value.voidsetValveSkip(boolean valveSkip)Set the valve skip flag.java.lang.StringtoString()String representation.
-
-
-
Field Detail
-
conditions
protected RewriteCond[] conditions
The conditions associated with this rule.
-
pattern
protected java.lang.ThreadLocal<java.util.regex.Pattern> pattern
The compiled pattern for matching.
-
substitution
protected Substitution substitution
The substitution to apply.
-
patternString
protected java.lang.String patternString
The pattern string.
-
substitutionString
protected java.lang.String substitutionString
The substitution string.
-
flagsString
protected java.lang.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!).
-
cookie
protected boolean cookie
This sets a cookie on the client's browser. The cookie's name is specified by NAME and the value is VAL. The domain field is the domain of the cookie, such as '.apache.org',the optional lifetime is the lifetime of the cookie in minutes, and the optional path is the path of the cookie
-
cookieName
protected java.lang.String cookieName
The cookie name.
-
cookieValue
protected java.lang.String cookieValue
The cookie value.
-
cookieDomain
protected java.lang.String cookieDomain
The cookie domain.
-
cookieLifetime
protected int cookieLifetime
The cookie lifetime.
-
cookiePath
protected java.lang.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 java.lang.ThreadLocal<java.lang.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 java.util.ArrayList<java.lang.String> envName
The environment variable names.
-
envValue
protected java.util.ArrayList<java.lang.String> envValue
The environment variable values.
-
envSubstitution
protected java.util.ArrayList<Substitution> envSubstitution
The environment substitutions.
-
envResult
protected java.util.ArrayList<java.lang.ThreadLocal<java.lang.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 inRewriteRule /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 java.lang.String typeValue
The MIME type value.
-
valveSkip
protected boolean valveSkip
Allows skipping the next valve in the Catalina pipeline.
-
-
Method Detail
-
parse
public void parse(java.util.Map<java.lang.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 java.lang.CharSequence evaluate(java.lang.CharSequence url, Resolver resolver)Evaluate the rule based on the context- Parameters:
url- The char sequenceresolver- Property resolver- Returns:
nullif no rewrite took place
-
toString
public java.lang.String toString()
String representation.- Overrides:
toStringin classjava.lang.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 java.lang.String getCookieName()
Get the cookie name.- Returns:
- the cookie name
-
setCookieName
public void setCookieName(java.lang.String cookieName)
Set the cookie name.- Parameters:
cookieName- the cookie name
-
getCookieValue
public java.lang.String getCookieValue()
Get the cookie value.- Returns:
- the cookie value
-
setCookieValue
public void setCookieValue(java.lang.String cookieValue)
Set the cookie value.- Parameters:
cookieValue- the cookie value
-
getCookieResult
public java.lang.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 java.lang.String getEnvName(int i)
Get the name of an environment variable.- Parameters:
i- the index- Returns:
- the environment variable name
-
addEnvName
public void addEnvName(java.lang.String envName)
Add an environment variable name.- Parameters:
envName- the name to add
-
getEnvValue
public java.lang.String getEnvValue(int i)
Get the value of an environment variable.- Parameters:
i- the index- Returns:
- the environment variable value
-
addEnvValue
public void addEnvValue(java.lang.String envValue)
Add an environment variable value.- Parameters:
envValue- the value to add
-
getEnvResult
public java.lang.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 java.lang.String getTypeValue()
Get the MIME type value.- Returns:
- the MIME type value
-
setTypeValue
public void setTypeValue(java.lang.String typeValue)
Set the MIME type value.- Parameters:
typeValue- the MIME type value
-
getPatternString
public java.lang.String getPatternString()
Get the pattern string.- Returns:
- the pattern string
-
setPatternString
public void setPatternString(java.lang.String patternString)
Set the pattern string.- Parameters:
patternString- the pattern string
-
getSubstitutionString
public java.lang.String getSubstitutionString()
Get the substitution string.- Returns:
- the substitution string
-
setSubstitutionString
public void setSubstitutionString(java.lang.String substitutionString)
Set the substitution string.- Parameters:
substitutionString- the substitution string
-
getFlagsString
public final java.lang.String getFlagsString()
Get the flags string.- Returns:
- the flags string
-
setFlagsString
public final void setFlagsString(java.lang.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 java.lang.String getCookieDomain()
Get the cookie domain.- Returns:
- the cookie domain
-
setCookieDomain
public void setCookieDomain(java.lang.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 java.lang.String getCookiePath()
Get the cookie path.- Returns:
- the cookie path
-
setCookiePath
public void setCookiePath(java.lang.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
-
-