3

I have a python app set up in cPanel. I have set the app URL to backend and when I query mydomain.com/backend/ it gives back the app's root view (which is just an html "hello there").

The rest of the endpoints are the ones I need but I get back a 404 on every other URL

@app.route('/')
def hello_world():
  logger.debug("Hi there")
  return "<h1 style='color:red'>Hi there</h1>"

@app.route('/test', methods=['POST','GET'])
def test():
    logger.info("Got test request")
    return {'ok' : 'success!'}
mydomain.com/backend/ --> Hi there
mydomain.com/backend/test --> 404
mydomain.com/backend/<any_other> --> 404

I would bet there's some other service returning the 404 before reaching the script. I could not find any reference to backend/ in the public_html/.htaccess. Dependencies are ok (no package complains).

My privileges on the server are very limited. Any pointers would be appreciated.

Thanks!

Ps found a similar question with no answers

Vladimir
  • 393
  • 3
  • 16
  • Does this answer your question? [Are a WSGI server and HTTP server required to serve a Flask app?](https://stackoverflow.com/questions/38982807/are-a-wsgi-server-and-http-server-required-to-serve-a-flask-app) – Ken Y-N Jul 26 '20 at 23:51
  • No. I believe my question is particularily bound to the cpanel way of mapping the app. – Vladimir Jul 27 '20 at 02:08

4 Answers4

4

Add the following rules to your .htaccess in the directory of your application url

RewriteEngine on  
RewriteRule ^http://%{HTTP_HOST}%{REQUEST_URI} [END,NE]

that solved this issue for me

1

I had the same problem with all routes(except the "/") in my Flask app returning a 500 error: Internal Server Error.

In my case, the .htaccess file had leftover code from the earlier days when I used a site builder apps on cpanel. Deleting the website builder projects did not delete the code in the .htaccess file and this legacy code was causing issues.

The code included the following:

RewriteEngine On
RewriteBase /
DirectoryIndex index.php index.cgi index.html
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^\/(js\/main\.js|css\/(\d+|common|site)\.css)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ncsitebuilder/$1 [L,QSA]

Disabling this code fixed the issue for me.

Amish
  • 23
  • 2
  • Had this exact same problem. 500s (not 404s) on every route except the root. Deleted the code and everything is linked up properly now. Youre a life saver my dude – WhiskeyHammer Jul 14 '22 at 00:51
0

A member of our team worked this out by adding a new subdomain and setting the url there.

So instead of mydomain.com/backend the app is running now at backend.mydomain.com/backend and working just fine.

Cheers

Vladimir
  • 393
  • 3
  • 16
0

You will need to modify the .htaccess file to achieve this. Since your application is in mydomain.com/backend/ when apache doesn't find it gives 404. To avoid this you need to use like the following which redirects all the requests to your app. Flask will handle the rest.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /backend/ [L]
orb
  • 43
  • 1
  • 10