# Redirections

The main idea of a redirection is telling to the web client to go to another URL when the request URL matchs some rule.

For example, if 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

The redir handler accepts three directives:

- Rewrite "regex" "subs"

This directive uses PCRE (Perl Compatible Regular Expressions) to make the substitution. The redirection will happen internally, hence the the internal URL in which the address is translated will be invisible for the client.

- Show Rewrite "regex" "subs"

It works in the same way as the previous one, but in this case, it will redirect the connection to the new resource.

- URL url

This specify the default rule which will be used if none of the previous Rewrite rules maches. It appends the requested URL to the end of url.

## Virtual hosts and redirections

The internals redirections, using the Rewrite key word, are limited to work in the same virtual host. All the internal redirections will be processed in the original virtual host, which makes quite sense in terms of security.

In the case you do need the redirect some resource to another virtual host and/or domain, you will have to use an explicit redirection using the Show Rewrite key words.

## Examples

This example will perform a hidden redirection:

    Directory /photo {
        Handler redir {
            Rewrite "/(\d+)$" "http://example.com/inst/photogallery/viewphoto?photoid=$1"
            Rewrite "/(\d+)/cmts" "http://example.com/viewcomments?photoid=$1"
            Rewrite "/(\d+)/delete" "http://example.com/inst/photogallery/admin?photoid=$1&method=delete"
            URL http://example.com/notfound?url=
        }
    }

Some examples of translations:

Request | Internal translation
/photo/123 | http://example.com/inst/photogallery/viewphoto?photoid=123
/photo/501/delete | http://example.com/inst/photogallery/admin?photoid=501&method=delete
/photo/something | http://example.com/notfound?url=something

It the request does not match on any of the Rewirte rules, it will use the default URL address to do the redirection.

In the case you want to use an HTTP redirection, this is the right configuration entry:

    Directory /photo {
        Handler redir {
            Show Rewrite "/(\d+)$" "http://example.com/inst/photogallery/viewphoto?photoid=$1"
            Show Rewrite "/(\d+)/cmts" "http://example.com/viewcomments?photoid=$"
            Show Rewrite "/(\d+)/delete" "http://example.com/inst/photogallery/admin?photoid=$1&method=delete"
            URL http://example.com/notfound?url=
        }
    }

In case you want to do a really simple redirection, you can use the URL tag as follows:

    Directory /stats {
        Handler redir {
            URL http://example.com/cgi-bin/awstats.pl
        }
    }

## Short path requests

There is a way to compact the Request entries that use a redir handler and a regular expression. For example, lets imagine we want to rewrite a request like /do/something to /index.php?action=something, the default syntax is:

    Request "^/do/.*/?" {
        Handler redir {
            Rewrite "^/do/(.*)/?" "/index.php?action=$1"
        }
    }

but it is also possible to use a short path request:

    Request "^/do/(.*)/?" {
        Handler redir {
            Rewrite "/index.php?action=$1"
        }
    }