Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redirect() uses server root instead of application root #293

Open
viernullvier opened this issue Mar 2, 2012 · 1 comment
Open

redirect() uses server root instead of application root #293

viernullvier opened this issue Mar 2, 2012 · 1 comment

Comments

@viernullvier
Copy link

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).

@iurisilvio
Copy link
Member

You can use bottle.get_url("named_route"). I guess it is what you want.

@route('/a/<y>', name="my_index")
def x(y): pass

bottle.get_url("my_index", y=1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants