Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Virtual Path Filter

This is not necessary in CherryPy 3 or better, as you can pass a script_name to cherrypy.tree.mount or cherrypy.quickstart.

Older versions

2.2

CherryPy 2.2 supported path-rewriting via request.objectPath.

import cherrypy

class VirtualPathFilter(object):
    """Filter that makes CherryPy ignorant of a URL root path.
    
    That is, you can mount your app so the URI "/users/~rdel/myapp/"
    maps to the root object "/".
    """
    
    def onStartResource(self):
        if cherrypy.config.get('virtualPathFilter.on', False):
            prefix = cherrypy.config.get('virtualPathFilter.prefix', '')
            if prefix:
                path = cherrypy.request.objectPath
                if path == prefix:
                    cherrypy.request.objectPath = '/'
                elif path.startswith(prefix):
                    cherrypy.request.objectPath = path[len(prefix):]
                else:
                    raise cherrypy.NotFound(path)

In your config file, include something like the following:

[global]
virtualPathFilter.on = True
virtualPathFilter.prefix = "/users/~rdel/myapp"

2.1

Add an instance of the following class to your _cpFilterList. It's buggy, but it will come close.

import cherrypy

class VirtualPathFilter(object):
    """Filter that makes CherryPy ignorant of the fact that it lives under some other
    webserver's URL space.
    
    Just turn of this filter to run CP standalone. You shouldn't have to change your
    URLs at all.

    Useful when running multiple sites within one CP server, or when running under
    mod_scgi. 
    """
    
    def beforeRequestBody(self):
        if cherrypy.config.get('virtualPathFilter.on', False):
            prefix = cherrypy.config.get('virtualPathFilter.prefix', '')
            if prefix:
                path = cherrypy.request.path
                if path == prefix:
                    path = '/'
                elif path.startswith(prefix):
                    path = path[len(prefix):]
                cherrypy.request.path = path


In your config file, include something like the following:

[/]
virtualPathFilter.on = True
virtualPathFilter.prefix = "/myapp/path"

Hosted by WebFaction

Log in as guest/cpguest to create tickets