... *Back to [[Book|Administrator's Guide#ag]] [[Topic|Administrator's Guide#cht9]]* h2. URL Redirection Using Regular Expression
Web Server is enhanced to support regular expressions (also known as Patterns) and request time parameter interpolation in configuration files. In addition, wildcard pattern matching support is extended to server.xml. URL redirecting is implemented as an SAF. The redirect SAF lets you redirect URIs that match a certain prefix. You can specify the prefix using the {{from}} parameter. You can specify the URL to redirect using the {{url}} or {{url-prefix}} parameters. In Web Server Web Server, the {{from}} parameter is optional. If {{from}} is omitted, all URIs are redirected.
In the obj.conf file, SAF parameters are supported with new {{<If>}}, {{<Elseif>}} and {{<Else>}} tags. These tags contain directives. Using these tags, you can define conditions under which the directives are executed. These tags can also be used to dynamically generate SAF parameters.
Web Server offers URL rewrite capability that is a super set of Apache HTTP server's {{mod_rewrite}} module. Unlike Apache's {{mod_rewrite}} function, {{<If>}} tag provides the following functionality:
* It can manipulate URI, path, header fields and response bodies. * It works at any stage of request processing. * It works with any SAF, including 3rd party plug-ins.
Consider the following directive:
{code} NameTrans fn="redirect" from="/site1" url="http://site1.mycompany.com" {code}
The above directive can be rewritten using regular expression as follows:
{code} <If $uri =~ '^/site1'> NameTrans fn="redirect" url="http://site1.mycompany.com" </If> {code}
In the above example, note the usage of regular expression instead of the {{from}} parameter. If you need to redirect all requests for {{/site1/*}} to {{http://site1.mycompany.com/*/index.html}} note this technique:
{code} <If $uri =~ '^/site1/(.*)'> NameTrans fn="redirect" url="http://site1.mycompany.com/$1/index.html" </If> {code}
Here, the {{<If>}} tag assigns whatever value matches (.*) to the variable $1. The $1 in the {{url}} parameter is dynamically replaced with the value from the original request. That means the above obj.conf example will cause a request for {{/site1/download}} to be redirected to {{http://site1.mycompany.com.com/download/index.html}}.
The combination of {{<If>}} and {{redirect}} offers some of the flexibility of {{mod_rewrite}}. However, unlike {{mod_rewrite}}, {{<If>}} can be used for things other than redirecting and rewriting URLs. {{<If>}} can also be used in conjunction with any SAF, including third party plug-ins.
The previously mentioned method configures a {{302 Moved Temporarily}} redirect. In Web Server , you can also add a {{status="301"}} parameter to indicate that you need a {{301 Moved Permanently}} redirect instead {code} NameTrans fn="redirect" from="/path" url="http://server.example.com" status="301" {code}
h3. What is Not Supported
Support for {{<If>}}, {{<Else>}} and {{<ElseIf>}} tags is limited in the Administration Infrastructure (Administration Console and CLI). Though {{obj.conf}} file supports these tags with any directives, variables, SAFs, expression literals, expression functions and expression operators, the Administration Infrastructure supports only {{<If>}} tags with redirect SAF for {{NameTrans}} directive with all the variables, expression literals, expression functions and expression operators.
For example, you can configure: {code} <If $browser =~ "MSIE"> NameTrans fn = "redirect" url="/msie.html" </If> {code}
|
... <Else> NameTrans fn="redirect" url="/other.html" </Else> {code}
{info:title=Note - }You can use the {{get-config-file}} and {{set-config-file}} CLI commands to make use of more complicated expressions using {{<If>}}, {{<ElseIf>}} and {{<Else>}}.
See {{get-config-file(1)}} and {{set-config-file(1)}} in CLI Reference Manual, [http://docs.sun.com/app/docs/prod/sjs.websrv70?l=en&a=view]. {info} |