Async web server and html suffix

Is there a "best" way to get asyncwebserver to serve static files when the URL does not contain the .html suffix?

  server.serveStatic("/", SPIFFS, "/");
  server.on("/demo", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/demo.html", "text/html");
  });

My SPIFFS filesystem contains index.html and webpage.html. From a browser, I can go to "http://esp" and get the base index.html. I can go to "http://esp/demo" and get demo.html. But if I go to "http://esp/webpage", it doesn't load webpage.html. I have a catch-all error handler that gives me "not found". I have to specify webpage.html to get it to load.

I understand that I can either rename all my *.html files as * (no .html extension) but then they are ambiguous and won't trigger html editor extensions. I can also create a handler for every file.html that I add to SPIFFS, but that's going to be error prone and tedious. I would like file.html to simply be served as a static file, which is what I thought serveStatic() would do.

Is there a cleaner way to do this?

It shouldn't upload either

"http://esp/demo"
should upload index.html from the demo folder.

"http://esp/webpage"
should upload index.html from the webpage folder.
.

Given that normal web resources have an extension, you could try using the regex functionality: if there's no dot, try .html. Something like (untested)

server.on("^\\/([^.]+)$", HTTP_GET, [] (AsyncWebServerRequest *request) {
  String html = request->url() + ".html";
  if (SPIFFS.exists(html)) {
    request->send(SPIFFS, html, "text/html");
  } else {
    request->send(404, "text/plain", "Not found");
  }
});

What happens if you send(SPIFFS a file that doesn't exist? Maybe you don't need to test yourself.

I'd argue that if /demo is a directory/folder, the web server should return a redirect to /demo/, and then the URL-path /demo/ should return the content of /demo/index.html and/or perhaps /demo/demo.html. If you have a bunch of index.html you have to look at the path to distinguish them; if what is immediately visible are the tabs in an editor for example, having unique names helps. Any distinct supporting files are more likely to named demo.js or demo.css, not index.

It isn't sending a redirect, is it? I mean, this IS the server, correct?

Edit: I don't think it sends a redirect for that. If it did, that would show in my address bar.

Not definitive, but just the first example that leapt to mind:

No slash at the end -- check before clicking -- but when you do, your address bar will end up at /v20.14.0/ with the slash at the end. Poke around a little, and you will see the redirect. A 301 Moved Permanently. BTW, same with

And since the content of those two are a bunch of subdirectories, those are all listed with a trailing slash to skip the redirect.

Thank you, that's great information and I understand where those paths are coming from now.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.