Sun Java System Web Server 7.0 Administrator's Guide

URL Redirection Using Regular Expression

Sun Java System Web Server 7.0 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 a SAF. The redirect SAF lets you redirect URIs that match a certain prefix. You can specify the prefix using the from parameter and the URL to redirect to using the url or url-prefix parameters. In Sun Java System Web Server 7.0, 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. See, Appendix - obj.conf - Syntax and Usage. 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.

Sun Java System Web Server 7.0 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:

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 snippet, 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 snippet will cause a request for /site1/download to be redirected to http://site1.mycompany.com.com/download/index.html.

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 above method configures a 302 Moved Temporarily redirect. In Sun Java System Web Server 7.0, 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"