Search code examples
djangoshared-hosting

Why is my hosted Django app showing my username in the URL?


I've written a Django app which I'm trying to get set up on shared web hosting (A2). It's working, except that when I go to:

http://example.com/terms/

the URL changes in the browser bar to:

http://example.com/home/myusername/myappfolder/myappname/terms/ 

showing the full path to where my app is on disk.

This doesn't happen with static files - e.g. http://example.com.com/static/image.png works normally.

The app is running in a virtual environment. I'm using python 3.6.8 and Django 2.1.4.

I followed these instructions to set up my app, which include setting up this passenger.wsgi file, that looks like this:

import myapp.wsgi
SCRIPT_NAME = '/home/username/myapp'

class PassengerPathInfoFix(object):
    """
    Sets PATH_INFO from REQUEST_URI because Passenger doesn't provide it.
    """
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        from urllib.parse import unquote
        environ['SCRIPT_NAME'] = SCRIPT_NAME

        request_uri = unquote(environ['REQUEST_URI'])
        script_name = unquote(environ.get('SCRIPT_NAME', ''))
        offset = request_uri.startswith(script_name) and len(environ['SCRIPT_NAME']) or 0
        environ['PATH_INFO'] = request_uri[offset:].split('?', 1)[0]
        return self.app(environ, start_response)

application = myapp.wsgi.application
application = PassengerPathInfoFix(application)

I'd be grateful for any pointers as to where to look to solve this.


Solution

  • Got it working!

    In my modified passenger_wsgi.py, I changed the line SCRIPT_NAME = os.getcwd() to SCRIPT_NAME = ''

    One thing I should point out is that the absolute path was getting inserted on redirects - so if I visited http://example.com/terms it would redirect to http://example.com/terms/ and insert the path in the URL.

    As you're debugging I recommend disabling the cache, as that threw me for several loops when changes I made didn't seem to take effect.

    Thanks to this question for getting me on the right track.