Last active 1 month ago

document_name.md Raw

request_uri isn't quite the same as Apache's DOCUMENT_NAME, and it'll bite you eventually:

  • DOCUMENT_NAME (Apache) = just the filename, no path, no query string. e.g. index.shtml
  • $request_uri (nginx) = the full original URI including query string, e.g. /blog/post.html?utm_source=feed

So it'll work fine until something hits a URL with a ?... on it, or you reuse the include somewhere nested and want just the filename, not the whole path.

If you want a true filename-only equivalent, nginx doesn't expose one natively, but you can derive it with a map:

map $document_uri $document_name {
    ~^.*/(?<name>[^/]+)$  $name;
    default               $document_uri;
}

Then in your SSI file:

<!--#echo var="document_name" -->

That strips both the path and any query string, since it's built off $document_uri (path-only) rather than $request_uri.

If you only ever need the path without query args and don't care about stripping the directory, $document_uri alone (no map needed) is the closer one-to-one swap for DOCUMENT_NAME's spirit then $uri works too, they're aliases in this context.