I am on a shared cpanel hosting plan that does not support wsgi apps directly. So I have to use the wsgiref CGIHandler workaround, as described here: http://flask.pocoo.org/docs/0.12/deploying/cgi/ .
It all works and produces expected results, but there is always this extra stuff in the urls: “/cgi-bin/index.cgi/” that the python app seems to be adding automatically (to match what it detects while called by the cgi handler).
For example, I would like it to be myhost.com/login/ instead of myhost.com/cgi-bin/index.cgi/login/, or myhost.com/ instead of myhost.com/cgi-bin/index.cgi/.
All those shorter versions of the links work well, because of the engine rewrite rules are in place. I have checked that. It is just a matter of finding a way to tell the flask app to get rid of “/cgi-bin/index.cgi/”.
Some of my code:
cat www/.htaccess
# Redirect everything to CGI WSGI handler, but Don't interfere with static files
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /cgi-bin/index.cgi/$1 [L]
.
cat www/cgi-bin/index.cgi
#!/home/myhost/myhost.com/flasky/venv/bin/python
import os
import sys
sys.path.insert(0, '/home/myhost/myhost.com/flasky/venv/lib/python2.7/site-packages')
sys.path.insert(0, '/home/myhost/myhost.com/flasky')
from wsgiref.handlers import CGIHandler
from manage import app
CGIHandler().run(app)
.
cat www/flasky/manage.py
#!/usr/bin/env python
import os
from app import create_app, db
from app.models import User, Role, Permission
from flask_script import Manager, Shell
from flask_migrate import Migrate, MigrateCommand
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
migrate = Migrate(app, db)
def make_shell_context():
return dict(app=app, db=db, User=User, Role=Role,
Permission=Permission)
manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
.
Any ideas?
Thanks!
2
Answers
I have found a hack for it.. but it's just a hack :-( If I override the value of SCRIPT_NAME environment variable in the wsgiref CGIHandler script, it works perfectly then.
Here's the updated code:
.
Basically, whatever the os.environ['SCRIPT_NAME'] value is, it gets prefixed to the flask app url every time a page is requested.
I am still looking for a more elegant "pythonic" solution though ..