Search code examples
pythonapachemod-rewritecherrypy

Apache mod_rewrite with multiple web apps


I have 2 separate cherrypy web apps I have written and need to put them both behind Apache with mod_rewrite or something similar. Need them to be accessed at http://domain.com/WebApp1 and http://domain.com/WebApp2. I figured out how to do a single virtual host so far, but it is only accessible at http://domain.com/. What would the correct configuration for Apache to get it to do this be? Should I be using something other than mod_rewrite?


Solution

  • You can avoid the usage of mod_rewrite if the two application are made with cherrypy.

    Mount each application in the cherrypy tree like this:

    import cherrypy
    
    from webapp1 import WebApp1
    from webapp2 import WebApp2
    
    cherrypy.tree.mount(WebApp1, '/WebApp1')
    cherrypy.tree.mount(WebApp2, '/WebApp2')
    cherrypy.engine.start()
    cherrypy.engine.block()
    

    For example:

     import cherrypy
    
     class AppOne(object):
         def index(self):
             return 'Hi from app one!'
         index.exposed = True
    
     class AppTwo(object):
         def index(self):
             return 'Hi from app two!'
         index.exposed = True
    
     if __name__ == '__main__':
         cherrypy.tree.mount(AppOne(), '/app1')
         cherrypy.tree.mount(AppTwo(), '/app2')
         cherrypy.engine.start()
         cherrypy.engine.block()
    

    or:

     import cherrypy
    
     class AppOne(object):
         def index(self):
             return 'Hi from app one!'
         index.exposed = True
    
     class AppTwo(object):
         def index(self):
             return 'Hi from app two!'
         index.exposed = True
    
     class Root(object):
         app1 = AppOne()
         app2 = AppTwo()
    
     if __name__ == '__main__':
         cherrypy.tree.mount(Root())
         cherrypy.engine.start()
         cherrypy.engine.block()
         # cherrypy.quickstart(Root()) # is the same
    

    Another alternative is to use mod_proxy.