IndexModulesHandlers

Handler: Redirection

The main idea of a redirection is telling the web client to go to another URL when the requested URL matches a rule. It could happen that this is not precisely what you are looking for. Check the generic balancer for layer 4 connection forwarding instead, or if you are looking for more complex layer 7 operations take a look at the reverse proxy handler. However, for URL redirections this handler is the place to go. A very common scenario would be rewriting URLS.

Lets suppose you have URLs like
    http://example.com/inst/photogallery/viewphoto?photoid=1235
    http://example.com/inst/photogallery/viewcomments?photoid=1235
    http://example.com/inst/photogallery/admin?photoid=1235&method=delete
You probably would prefer URLs like
    http://example.com/photo/1235
    http://example.com/photo/1235/cmts
    http://example.com/photo/1235/delete

Parameters

This directive uses PCRE (Perl Compatible Regular Expressions) to make the substitution.

Virtual hosts and redirections

The internal redirections, using the internal keyword, are limited to work in the same virtual host. All the internal redirections will be processed in the original virtual host, which makes a lot of sense in terms of security.

In case you do need to redirect a resource to another virtual host and/or domain, you will have to use an explicit redirection using the external keyword.

The influence of the rule type

Any rule type can be used with the redirection handler: Directory, Extensions, Regular Expression, etc. Only the case where the type is Regular Expression will require some more extra information. Because of Cherokee's design, modularity and inheritance is heavily used throughout the code base. This one is a special case because the regex entry of the redirection handler can be empty whenever the matching rule used is a regular expression (because it is inherited by it).

The logic behind this is that you can set up a regular expression to match the rule, but once you're in the redirection handler you could or could not be needing another matching to be performed. If not, the original one (the one from the rule) will be enough. If you do, you can specify a completely different expression to be matched. For instance, you could match the rule taking just the beginning part of the request as meaningful, and once you are into the handler you might want to consider just the end of it before making the substitution.

Examples

This example will perform internal redirections:

Regular Expression Substitution
/(\d+)$ http://example.com/inst/photogallery/viewphoto?photoid=$1
/(\d+)/cmts http://example.com/viewcomments?photoid=$1
/(\d+)/delete http://example.com/inst/photogallery/admin?photoid=$1&method=delete

Which would translate into the following redirections for the listed matching requests:

Request Internal translation
/photo/123 http://example.com/inst/photogallery/viewphoto?photoid=123
/photo/213/cmts http://example.com/viewcomments?photoid=213
/photo/501/delete http://example.com/inst/photogallery/admin?photoid=501&method=delete