Class RewriteRule
java.lang.Object
org.apache.catalina.valves.rewrite.RewriteRule
Represents a rewrite rule for URL rewriting.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanThis flag chains the current rule with the next rule (which itself can be chained with the following rule, etc.).protected RewriteCond[]The conditions associated with this rule.protected booleanThis sets a cookie on the client's browser.protected StringThe cookie domain.protected booleanWhether the cookie is HTTP only.protected intThe cookie lifetime.protected StringThe cookie name.protected StringThe cookie path.protected ThreadLocal<String> The cookie result.protected booleanWhether the cookie is secure.protected SubstitutionThe cookie substitution.protected StringThe cookie value.protected booleanThis 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.The environment variable names.protected ArrayList<ThreadLocal<String>> The environment results.protected ArrayList<Substitution> The environment substitutions.The environment variable values.protected StringThe flags string.protected booleanThis forces the current URL to be forbidden, i.e., it immediately sends back an HTTP response of 403 (FORBIDDEN).protected booleanThis forces the current URL to be gone, i.e., it immediately sends back an HTTP response of 410 (GONE).protected booleanHost.protected booleanStop the rewriting process here and don't apply any additional rewriting rules.protected booleanRe-run the rewriting process (starting again with the first rewriting rule).protected booleanThis 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 booleanThis flag keeps mod_rewrite from applying the usual URI escaping rules to the result of a rewrite.protected booleanThis flag forces the rewriting engine to skip a rewriting rule if the current request is an internal sub-request.protected ThreadLocal<Pattern> The compiled pattern for matching.protected StringThe pattern string.protected booleanWhether the pattern is positive (true) or negated (false).protected booleanThis flag forces the rewriting engine to append a query string part in the substitution string to the existing one instead of replacing it.protected booleanWhen 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 booleanPrefix Substitution with http://thishost[:thisport]/ (which makes the new URL a URI) to force an external redirection.protected intThe redirect HTTP status code.protected intThis flag forces the rewriting engine to skip the next num rules in sequence when the current rule matches.protected SubstitutionThe substitution to apply.protected StringThe substitution string.protected booleanForce the MIME-type of the target file to be MIME-type.protected StringThe MIME type value.protected booleanAllows skipping the next valve in the Catalina pipeline. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCondition(RewriteCond condition) Add a condition to this rule.voidaddEnvName(String envName) Add an environment variable name.voidaddEnvValue(String envValue) Add an environment variable value.evaluate(CharSequence url, Resolver resolver) Evaluate the rule based on the contextGet the conditions for this rule.Get the cookie domain.intGet the cookie lifetime.Get the cookie name.Get the cookie path.Get the cookie result.Get the cookie value.getEnvName(int i) Get the name of an environment variable.getEnvResult(int i) Get the result of an environment variable substitution.intGet the number of environment variables.getEnvValue(int i) Get the value of an environment variable.final StringGet the flags string.Get the pattern string.intGet the redirect HTTP status code.intgetSkip()Get the number of rules to skip.Get the substitution.Get the substitution string.Get the MIME type value.booleanisChain()Get whether this rule is chained.booleanisCookie()Get whether the cookie flag is set.booleanGet whether the cookie HTTP only flag is set.booleanGet whether the cookie secure flag is set.booleanisEnv()Get whether the env flag is set.booleanGet whether back references are escaped.booleanGet 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.booleanGet whether the noescape flag is set.booleanGet whether the nosubreq flag is set.booleanGet whether the qsappend flag is set.final booleanGet whether the qsdiscard flag is set.booleanGet whether the redirect flag is set.booleanisType()Get whether the type flag is set.booleanGet whether the valve skip flag is set.voidparse(Map<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(String cookieDomain) Set the cookie domain.voidsetCookieHttpOnly(boolean cookieHttpOnly) Set the cookie HTTP only flag.voidsetCookieLifetime(int cookieLifetime) Set the cookie lifetime.voidsetCookieName(String cookieName) Set the cookie name.voidsetCookiePath(String cookiePath) Set the cookie path.voidsetCookieSecure(boolean cookieSecure) Set the cookie secure flag.voidsetCookieValue(String cookieValue) Set the cookie value.voidsetEnv(boolean env) Set the env flag.voidsetEscapeBackReferences(boolean escapeBackReferences) Set whether back references are escaped.final voidsetFlagsString(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(String patternString) Set the pattern string.voidsetQsappend(boolean qsappend) Set the qsappend flag.final 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(String substitutionString) Set the substitution string.voidsetType(boolean type) Set the type flag.voidsetTypeValue(String typeValue) Set the MIME type value.voidsetValveSkip(boolean valveSkip) Set the valve skip flag.toString()String representation.
-
Field Details
-
conditions
The conditions associated with this rule. -
pattern
The compiled pattern for matching. -
substitution
The substitution to apply. -
patternString
The pattern string. -
substitutionString
The substitution string. -
flagsString
The flags string. -
positive
protected boolean positiveWhether the pattern is positive (true) or negated (false). -
chain
protected boolean chainThis 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 cookieThis 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
The cookie name. -
cookieValue
The cookie value. -
cookieDomain
The cookie domain. -
cookieLifetime
protected int cookieLifetimeThe cookie lifetime. -
cookiePath
The cookie path. -
cookieSecure
protected boolean cookieSecureWhether the cookie is secure. -
cookieHttpOnly
protected boolean cookieHttpOnlyWhether the cookie is HTTP only. -
cookieSubstitution
The cookie substitution. -
cookieResult
The cookie result. -
env
protected boolean envThis 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
-
envValue
-
envSubstitution
The environment substitutions. -
envResult
The environment results. -
forbidden
protected boolean forbiddenThis 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 goneThis 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 hostHost. 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 lastStop 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 nextRe-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 nocaseThis 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 noescapeThis 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 nosubreqThis 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 qsappendThis 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 qsdiscardWhen 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 redirectPrefix 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 redirectCodeThe redirect HTTP status code. -
skip
protected int skipThis 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 typeForce 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
The MIME type value. -
valveSkip
protected boolean valveSkipAllows skipping the next valve in the Catalina pipeline.
-
-
Constructor Details
-
RewriteRule
public RewriteRule()Default constructor.
-
-
Method Details
-
parse
Parse the rule using the provided rewrite maps.- Parameters:
maps- the rewrite maps
-
addCondition
Add a condition to this rule.- Parameters:
condition- the condition to add
-
evaluate
Evaluate the rule based on the context- Parameters:
url- The char sequenceresolver- Property resolver- Returns:
nullif no rewrite took place
-
toString
-
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
Get the conditions for this rule.- Returns:
- the conditions array
-
setConditions
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
-
setCookieName
Set the cookie name.- Parameters:
cookieName- the cookie name
-
getCookieValue
-
setCookieValue
Set the cookie value.- Parameters:
cookieValue- the cookie value
-
getCookieResult
-
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
Get the name of an environment variable.- Parameters:
i- the index- Returns:
- the environment variable name
-
addEnvName
Add an environment variable name.- Parameters:
envName- the name to add
-
getEnvValue
Get the value of an environment variable.- Parameters:
i- the index- Returns:
- the environment variable value
-
addEnvValue
Add an environment variable value.- Parameters:
envValue- the value to add
-
getEnvResult
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
-
setSubstitution
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
-
setTypeValue
Set the MIME type value.- Parameters:
typeValue- the MIME type value
-
getPatternString
-
setPatternString
Set the pattern string.- Parameters:
patternString- the pattern string
-
getSubstitutionString
-
setSubstitutionString
Set the substitution string.- Parameters:
substitutionString- the substitution string
-
getFlagsString
-
setFlagsString
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
-
setCookieDomain
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
-
setCookiePath
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
-