3 ms·
This is one of those "made up problems" as it only exists because you insist in doing magic, i.e. mapping /foo to either /foo/index.html or /foo.html. I am gui
by brabel 5y ago
This is one of those "made up problems" as it only exists because you insist in doing magic, i.e. mapping /foo to either /foo/index.html or /foo.html.
I am guilty myself, because we need to do things that match expectation of users, but this problem simply disappears if you stop doing magic.
So, the real question is, what "magic" do you prefer?
My implementation was, when given a trailing slash, check if the recource is a directory (as this is what the trailing slash signifies) and use the convention of loading the index.html file at that directory if it is. If it's not a directory, then it's a 404.
If there's no trailing slash, try to load the file with the equivalent extension to the Accept content-type header. So, if the client wants JSON, you try /foo.json for the path /foo. If client requests HTML, try /foo.html.
- dotancohen 5y ago> This is one of those "made up problems" as it only exists because you insist in doing magic, i.e. mapping /foo to either /foo/index.html or /foo.html. If the underlying resource (web server) is Linux, then there is no magic. In Linux, everything is a file, including directories (maybe in Unix too, I don't know, I only work with Linux). So e.g. `/var/www/foo` is the same resource as `/var/www/foo/`. But without the context of the underlying filesystem being available to query that (e.g. with the `file` command) there is no way to know that `/foo` refers to a directory.
- brabel 5y agoWhen `index.html` is a file, does `ls index.html` show the same result for you as `ls index.html/`?
- dotancohen 5y agoI see, yes, if the server decides to send index.html when `index.html/` is requested, that is "magic" and your point is very valid.
- Yaggo 5y ago> i.e. mapping /foo to either /foo/index.html or /foo.html Urls lost their 1:1 mapping to physical files a long time ago. Nowadays they are mostly just arbitrary strings, although well-crafted ones may reflect the hierarchy of content in their structure.
- brabel 5y ago> Urls lost their 1:1 mapping to physical files a long time ago. Says who? Nearly every web server in existence supports static assets which map 1-to-1 to the file system.
- elsjaako 5y agoThis assumes that you are serving webpages from a directory with HTML and php files. With many modern services (for example, anything written in django) this isn't how it works.