Last active 1 month ago

brennan revised this gist 1 month ago. Go to revision

1 file changed, 25 insertions

document_name.md(file created)

@@ -0,0 +1,25 @@
1 + `request_uri` isn't quite the same as Apache's `DOCUMENT_NAME`, and it'll bite you eventually:
2 +
3 + - `DOCUMENT_NAME` (Apache) = just the filename, no path, no query string. e.g. `index.shtml`
4 + - `$request_uri` (nginx) = the full original URI *including* query string, e.g. `/blog/post.html?utm_source=feed`
5 +
6 + 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.
7 +
8 + If you want a true filename-only equivalent, nginx doesn't expose one natively, but you can derive it with a `map`:
9 +
10 + ```nginx
11 + map $document_uri $document_name {
12 + ~^.*/(?<name>[^/]+)$ $name;
13 + default $document_uri;
14 + }
15 + ```
16 +
17 + Then in your SSI file:
18 +
19 + ```html
20 + <!--#echo var="document_name" -->
21 + ```
22 +
23 + That strips both the path and any query string, since it's built off `$document_uri` (path-only) rather than `$request_uri`.
24 +
25 + 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.
Newer Older