title: Flask function url_for doesn't work for sub-domain deployment on Bluehost
body: <p>I deployed a very simple Flask application called 'app' in a sub directory under root directory in Bluehost. Hopefully, example.com points to the homepage and example.com/app points to my Flask application. Actually, the Flask application works pretty fine when the script index.py looks like:</p>
  
  <pre><code>from flask import Flask
  
  app = Flask(__name__)
  
  @app.route('/', methods=['GET'])
  def home():
      return 'Hello world'
  
  if __name__ == "__main__":
      app.run()
  </code></pre>
  
  <p>But things went bad as I introduced a simple login functionality, with the doc structure and index.py look like:</p>
  
  <p>doc structure:</p>
  
  <pre><code>public_html                     
  |--app                          
       |--.htaccess
       |--index.fcgi
       |--index.py
       |--static
          |--login.html
       |--templates
          |--home.html
  </code></pre>
  
  <p>index.py:</p>
  
  <pre><code>from flask import Flask, url_for, request, render_template, redirect, session
  app = Flask(__name__)
  
  @app.route('/', methods=['GET'])
  def home():
      if not session.get('user'):
          return redirect(url_for('login'))     #go to login page if not logined
      else:
          return render_template('home.html')   #otherwise go to home page
  
  @app.route('/login', methods=['GET', 'POST'])
  def login():
      if request.method == 'GET':
          return app.send_static_file('login.html')
      else:
          user = request.form.get('user')
          password = request.form.get('password')
          if user == 'joy' and password == 'joy':
              session['user'] = user
              return render_template('home.html')
          else:
              return 'LOGIN FAILED'
  
  if __name__ == "__main__":
      app.run()
  </code></pre>
  
  <p><strong>However, accessing example.com/app led to a changed URL as example.com/login and a reasonable 404 error as example.com/login doesn't map to any document.</strong> </p>
  
  <pre><code>return redirect(url_for('login'))
  </code></pre>
  
  <p><strong>The url_for('login') return example.com/login instead of example.com/app/login.</strong> That's why the latter version of index.py doesn't work. I tried so many things but didn't came across any fix. Please help. THanks!</p>
  
  <p>My .htaccess:</p>
  
  <pre><code>Options +ExecCGI
  AddHandler fcgid-script .fcgi
  RewriteEngine On
  #RewriteBase /app/        # Neither RewriteBase / or RewriteBase /app/  work
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.fcgi/$1 [QSA,L]
  </code></pre>
  
  <p>My index.fcgi:</p>
  
  <pre><code>import sys
  sys.path.insert(0, '/path_to_my_python_site-packages')
  
  from flup.server.fcgi import WSGIServer
  from index import app
  
  class ScriptNameStripper(object):
     def __init__(self, app):
         self.app = app
  
     def __call__(self, environ, start_response):
         environ['SCRIPT_NAME'] = ''
         return self.app(environ, start_response)
  
  app = ScriptNameStripper(app)
  
  if __name__ == '__main__':
      WSGIServer(app).run()
  </code></pre>