|
|
|
How to (ab)use the <redirect> tag It is a common task to redirect the client browser to some URL based on certain conditions: login name, password or as a part of handling the form errors. Caudium defines a useful tag called <redirect> for this purpose. The tag, despite its usefulness, has two quirks that may sometimes come in your way, namely:
Both issues have their remedies which are shown below. The POST or GET variables are passed to the new URIPassing the variables along is sometimes a useful feature, but at other times it might be an obstacle. The only way to get rid of them from the new URI is to use two other Caudium tags, namely <header> and <return>. The excerpt of RXML code below shows exactly how it can be done: <header name="Location" value="http://caudium.net"> <return code="301"> What is happening there is that Caudium adds a new header to the response sent to the browser which looks like this: And the return code for this page is set to 301 which is a HTTP code telling the browser that the page was permanently moved to the given Location: URI. Setting the header has the additional effect that the URI is taken verbatim and, unlike with the <redirect> tag, no variables (either POST or GET ones) will be passed to the new location.Location: http://caudium.net/ First, a few words describing the problem. Sometimes it is useful to use several redirects in one page to handle various situations - errors, exceptions etc. The problem with the <redirect> tag, as stated above, is that it resets the target location on every invocation. That makes it impossible to use different redirect URIs since Caudium always parses the entire page - it doesn't stop parsing when the <redirect> tag is found. To see a demonstration of this, cut and paste the code below to some HTML file and open it from your browser (the server must run Caudium, of course :-)): You will notice that you end up with the /(test2)/ URI but with just one redirect.<redirect to="/(test)/"> <redirect to="/(test2/"> Solution of this issue is a bit of a hack - not quite the cleanest one, but pretty straight forward and not complicated. The idea is to set a flag (variable) whenever you set a redirect and then check that variable before modifying the redirect. Here is a simple code that takes care of the problem: and so on with every redirect you use. The same can be achieved by using the <header> and <return> code show above instead of the <redirect> invocation. Of course, the whole sequence can be put in a <define> macro which will save you some typing. Doing that is left as an exercise to the gentle Reader :).<redirect to="/(test)/"> <set variable="redirected" value="yes"> <if not variable="redirected"> <redirect to="/(test2)/"> <set variable="redirected" value="yes"> </if> |
|
Copyright © 2000 - 2005
The Caudium Group
All Rights Reserved. Hosting by Kazar.
|
|