defnull/bottle — Issue #293: redirect() uses server root instead of application root
Author: viernullvier
Created: 2012-03-02 13:32:10+00:00
State: open

I've run into an issue when using bottle in a subdirectory of the server root.

Steps to reproduce:
1. Build a simple app in a subdirectory of the server root (CGI works good for that).
2. Put `redirect("/")` into a route.
3. Call the route. You'll be redirected to the server root instead of the application root.

This issue makes it difficult to build apps that are deployed in different paths. The cause of the problem is that `redirect()` uses `urljoin()` on the request URL. If the target contains a leading slash, everything after the host part of the URL gets replaced. This could also lead to problems when using `mount()`, though I haven't tested it yet.

Workarounds:
* Build your own redirect function that prepends the application root directory
* Add `request.script_name` to every redirect (ugly)
* modify `redirect()` so that it always includes `request.script_name` (hackish)

The problem with `request.script_name` is that in some cases it includes the name of the .py file which would lead to ugly URLs, especially when using CGI on Apache with mod_rewrite. A more sustainable solution would be the introduction of a application root variable (which can be "/" by default). The root path could be set in `run()` or in a special function. When it is set, all redirects are based on that path.

One could also add the path to SimpleTemplate.defaults to include it in template URLs, but a more beautiful solution for that would be a special markup for URLs (maybe `{# #}` or similar).