Regular Expression Redirects in Web Server 7
By elving on May 16, 2006
The Sun Java System Web Server 7 Technology Preview was released today! There's a whole bunch of new stuff in 7, and you can use it free of charge.
One of the features I worked on is the <If> tag. As its name implies, <If> makes things conditional. There's a lot of hidden power in the <If> tag, though, especially when combined with regular expressions. When you pair <If> with the redirect SAF, you get some of the flexibility of Apache HTTP Server's mod_rewrite module.
In Web Server, features such as URL forwarding are implemented as Server Application Functions (SAFs). Back in 1994 when Web Server was named Mosaic Netsite, Rob McCool checked in the first version of the redirect SAF. In the decade that followed, redirect learned how to deal with path parameters and query strings. Beyond that, it was largely unchanged.
The redirect SAF lets you redirect URIs that match a certain prefix. (A URI is the part of the URL a web browser sends in its HTTP request.) You specify that prefix with the
from parameter and the URL to redirect to with the either the
url-prefix parameters. In Web Server 7, the
from parameter is now optional. If
from is omitted, all URIs are redirected. Obviously, that's not particularly useful by itself, but it means you can use <If> to decide which URIs to redirect.
For example, you might have used the following directive in obj.conf in Sun ONE Web Server 6.1:
NameTrans fn="redirect" from="/docs" url="http://docs.sun.com"
With Sun Java System Web Server 7, you can use a regular expression instead of the
<If $uri =~ '\^/docs'> NameTrans fn="redirect" url="http://docs.sun.com" </If>
Given the weird syntax and additional line, I guess that's not really an improvement.
However, suppose you want to do something a little fancier; maybe you want to redirect all requests for /docs/partnumber to http://docs.sun.com/source/partnumber/index.html. With the <If> tag and regular expressions, you can do that:
<If $uri =~ '\^/docs/
(.\*)'> NameTrans fn="redirect" url="http://docs.sun.com/source/
Here, the <If> tag assigns whatever value matches
(.\*) to the variable
$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 /docs/817-6248 to be redirected to http://docs.sun.com/source/817-6248/index.html. (You can get even fancier. For example, if you added a second set of parentheses to your regular expression, <If> would also create a variable named
$2 whenever the regular expression matched the requested URI.)
I mentioned that the combination of <If> and redirect offers only some of the flexibility of mod_rewrite. That's true. However, unlike mod_rewrite, <If> can be used for things other than redirecting and rewriting URLs. In fact, <If> can be used in conjunction with any SAF, including 3rd party plugins. All told, Web Server 7 now offers a superset of mod_rewrite's functionality.
In the coming weeks I hope to write about other ways to use <If> and introduce other new features in Web Server 7. Why not try it out and let me know what you think?