0

Hello Stackoverflow'ers

Have a routing problem with flask and the local apache server I have set up and none of the solutions I've found on the net works. I'm stuck on how to solve it and asks the question here.

On apache I have the following setup:

WSGIScriptAlias /programmet /path/to/where/script/is/located/script.wsgi

and have set up the local address for the server to be: server.x

I'll make two examples of what I have made in flask:

program = Flask(__name__, template_folder="html")

@program.route("/")
def start():
    return render_template("index.html")

@program.route("/log-in")
def login():
    return render_template("login.html")

Upon writing server.x/programmet/, the scripts starts and works but the routes doesn't work att all when clicking on the links made in the html file.

What I see is that the routes are linking to the server root not the script root. IE when clicking on the following links:

<"a href="/"">"Start"<"/a">
<"a href="/log-in"">"Log in"<"/a">

It doesn't go to server.x/programmet/ or server.x/programmet/log-in. Instead the routing tries to connect to the following adresses: server.x and server.x/log-in.

How can I solve the problem and get the correct routes in Flask?


EDIT

Have to do an edit on the original post based on the excellent tips furas gave me.

The tips he gave me didn't work for a start BUT this was when running the apache server.

I downloaded the gunicorn server and tried the script there. It worked like a charm. All links through Flask was linked correctly.

Obviously there is something wrong on the server side (that will say the apache server). So is it anybody that knows how to fix the issue with the apache?

werner
  • 1
  • 4
  • if you want to use `WSGIScriptAlias /programmet` then in HTML you have to set `/programmet` instead of `/` and `/programmet/log-in` instead of `/log-in` - `Flask` will not do it for you and you have to change it manually. Eventually you can use relative paths in HTML - without starting `/` (ie. `log-in` without `/`) and then it will use currect `/programmet` plus relative `log-in` - but sometimes it may give wrong results. – furas Sep 25 '20 at 08:10
  • I tried to change the routing as you said. Now the login works but the former just happens the same thing: it routes back to server root. – werner Sep 25 '20 at 08:30
  • if you use `redirect()` then you have to use `/programmet` – furas Sep 25 '20 at 08:35
  • see also question [Add a prefix to all Flask routes](https://stackoverflow.com/questions/18967441/add-a-prefix-to-all-flask-routes) – furas Sep 25 '20 at 08:38
  • Thanks furas for your time. I'm still stuck. I have no redirects active in the script. I tried to change the routings but one route works properly whilst the other redirects to server root. I tried program.config["APPLICATION_ROOT"]="/programmet". That didn't work either. I just got the same behaviour as earlier. Will try the remaining part of what's written in the link you provided. – werner Sep 25 '20 at 08:47
  • maybe you have redirect in HTML/JavaScript ? – furas Sep 25 '20 at 09:05
  • I have no redirects in the html code. I don't know javascript so there is no javascript in the html either. – werner Sep 25 '20 at 09:23

0 Answers0