Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Flask gives 404 for files in the same directory with my application

Let's say I have a domain. Under home directory of the domain I have a text(.txt) file called note.txt. Like below

https://www.example.com/note.txt

When I access the url, browser display text string contained inside the file. But when I run a Flask under that domain instead of a traditional html,css,javascript,php app, server return 404error even though the file exists in fact at the same location. I can see this from the ftp client.

So why does the server returns 404 error when the site hosts a python app instead of the more traditional html,css,javascript,php app?

like image 271
Zip Avatar asked Dec 29 '25 22:12

Zip


1 Answers

What you are missing here is that Flask has its own URL routing.

  • Python code must be exposed through app.route decorator

  • Static files, like your note.txt can be served through Flask, but they are often handled by the front end web server (Nginx, Apache) through their configuration

The answer for "why does the server returns 404 error" is that URL routing should be explicit (nothing happens unless you tell it to happen) instead of implicit (everything on the server is exposed by default). Because PHP chose the latter approach, WordPress, Drupal, et. al. traditional PHP sites are getting hacked very easily when they are given to people who don't have the full picture what they are doing. It might be convenient in the beginning, but it is also an open invitation for script kiddies to raid your server.

like image 107
Mikko Ohtamaa Avatar answered Jan 01 '26 10:01

Mikko Ohtamaa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!