I hope you can help with the following as I’ve been struggling to make it work for many days now.
To give you a little background, I used to host a flask app on my website domain.xyz. It is hosted on namecheap and runs via the CPanel Python App.
I then decided to also have a WordPress on the same site, so I had to move the flask to the subdomain api.domain.xyz
Now, the WordPress site works, as does the root page of the flask app (api.domain.xyz). However, all other routes of the flask give me the same error page:
„ Not Found
The requested URL was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.“
Im Hosting with Namencheap, and have tried many times the support, it they insist that it’s a code Problem. What am I doing wrong?
Here’s the python code:
#main.py
from flask import Flask, jsonify, request, abort
import os
from flask import g
import time
import socket
app = Flask(__name__, subdomain_matching=True)
#app.config["APPLICATION_ROOT"] = "/api"
#app.config["DEBUG"] = True
@app.before_request
def before_request():
print("before request")
g.start = time.time()
@app.route('/test', subdomain="api", methods=['GET'])
def test():
return '''api'''
@app.route('/', methods=['GET'])
def home():
print("HOME")
return '''<p>This is the root page.</p>'''
@app.after_request
def after_request(response):
diff = time.time() - g.start
print ("Request time: " + str(diff))
if ((response.response) and
(200 <= response.status_code < 300) and
(response.content_type.startswith('text/html'))):
response.set_data(response.get_data().replace(
b'__EXECUTION_TIME__', bytes(str(diff), 'utf-8')))
return response
if __name__ == '__main__':
app.run(host='api.domain.xyz', debug=True)
So basically, api.domain.xyz routs properly and writes „this is the root page“, while api.domain.xyz/test shows the error. In particular, not even the before_request code gets called (as I can tell from the logs), which tells me it never makes it to the code.
Any help is hugely appreciated!
3
Answers
It turned out that the code was OK, however the folder structure was not correct. It seems that for every route in flask, there needs to be the respective folders in the folder structure. In my case, where I had before
I had to add a separate folder for every route, in my example above:
Were you to add deeper routes (say "@app.route('/test/v1/resources', subdomain="api", methods=['GET'])", you'd need to add the folder
as well.
Not entirely sure if this is your issue but flask may be failing to find your flask program so in the directory with the program in (as well as any virtual environment used is activated) run:
You can test if it found your program by running
Sorry if this does not work but this is a fairly simple solution but has happened a few times.
1 – A site in wordpress in the root of the directory, which has a standard wordpress .htaccess.
2 – An application running in the subfolder "panel", which has a .htaccess and works with the cpanel API to create subdomains in that same folder.
Problem:
When the cPanel API is triggered, the wordpress .htaccess file does not let the API enter the subfolder "panel" and create the subdomain. When I remove the .htaccess file from wordpress from the root, the API works 100%
Solution--
may be this will work:
I am using godaddy deluxe hosting and its working in my case.
in your folder create a .htaccess and put this code:
or
in your main .htaccess file you can use: