Flow action redirect

The redirect action sends HTTP redirects to the client and terminates the request.

A redirect action has the following attributes:

  • status="301|302|303|307" to set the HTTP status code (optional, default is 302)
  • rewrite="true|false" to rewrite the URL to a FIT URL (optional, default is true)
  • Either location="..." or xpath="..." to define the target URL (required)
  • content="..." to set a response body. The value is either the id of a request (e.g. main) or a fit URI (e.g. fit://site/templates/redirect.html) (optional, by default no content is set)

Examples

<redirect location="http://example.com/" content="fit://site/public/info.html" />
<redirect status="301" rewrite="false" xpath="'http://example.com/'" content="main" />

Note that the string values in xpath attributes must be quoted with ticks (see the examples above).

XPath gives you a lot of flexibility to define the redirect URL. A common use case is reading the URL from the loaded main content:

<html>
  <body>
    <a id="myLink" href="http://example.com/help">http://example.com/info</a>
  </body>
</html>

Examples of various possibilities to select an URL with XPaths from the DOM above:

<!-- Use of element content with explicit string cast -->
<redirect xpath="string(fit-document('fit://request/content')//a[@id='myLink'])" /> <!--http://example.com/info -->

<!-- Use of element content with implicit string cast by the redirect action -->
<redirect xpath="fit-document('fit://request/content')//a[@id='myLink']" /> <!--http://example.com/info -->

<!-- Use of attribute content with implicit string cast -->
<redirect xpath="fit-document('fit ://request/content')//a[@id='myLink']/@href" /> <!--http://example.com/help -->

If the redirect URL returned by location or xpath is relative, it will be resolved with the requests’s main URL. If the URL is read from content other than the main content, the resolving may be wrong. You could use concat() instead.

If you want to prevent resolving (and thereby rewriting to FIT, too) you may carry out the redirect manually:

<set-header name="Location" value="/help" />
<dump in="fit://request/empty" />

Errors

A redirect action always terminates the request, even if no redirect may be sent to the client.

The following cases are considered errors:

  • Redirect URLs within the fit:// protocol with rewrite="false" option (because the protocol is unknown to the client)
  • Redirect URLs to hosts with no mapping in a mandatory conf/urlmap.xml and rewrite="true".
  • Empty location
  • Broken xpath expressions
  • XPaths that do not return a string or a node