URL Redirection Using Regular Expression

                                                                                                                                                                                                                                                                                                                                      Back to [Book]     [Topic]

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:

NameTrans fn="redirect"
                    from="/site1"
                 url="http://site1.mycompany.com"

The above directive can be rewritten using regular expression as follows:

<If $uri =~ '^/site1'>
           NameTrans fn="redirect"
           url="http://site1.mycompany.com"
</If>

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:

<If $uri =~ '^/site1/(.*)'>
           NameTrans fn="redirect"
           url="http://site1.mycompany.com/$1/index.html"
</If>

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

NameTrans fn="redirect" from="/path" url="http://server.example.com" status="301"

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:

<If $browser =~ "MSIE">
    NameTrans fn = "redirect" url="/msie.html"
</If>

But you cannot configure:

<If $browser =~ "MSIE">
    NameTrans fn = "redirect" url="/msie.html"
</If>
<Else> NameTrans fn="redirect" url="/other.html" </Else>
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.

Labels

java java Delete
server server Delete
administrator's administrator's Delete
sun sun Delete
webserver webserver Delete
application application Delete
system system Delete
webtier webtier Delete
guide guide Delete
webserver70 webserver70 Delete
sunjava sunjava Delete
administratorsguide administratorsguide Delete
+ag +ag Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Sign up or Log in to add a comment or watch this page.


The individuals who post here are part of the extended Sun Microsystems community and they might not be employed or in any way formally affiliated with Sun Microsystems. The opinions expressed here are their own, are not necessarily reviewed in advance by anyone but the individual authors, and neither Sun nor any other party necessarily agrees with them.

Copyright 1994-2009 Sun Microsystems, Inc.
Powered by Atlassian Confluence
Sun Guidelines on Public Discourse Privacy Policy Terms of Use Trademarks Site Map Employment Investor Relations Contact