Skip to content

Instantly share code, notes, and snippets.

@mhuber84
Created February 14, 2017 08:29
Show Gist options
  • Save mhuber84/66f4e11af3c84ab91180a841c5d02911 to your computer and use it in GitHub Desktop.
Save mhuber84/66f4e11af3c84ab91180a841c5d02911 to your computer and use it in GitHub Desktop.
Set download filename to pre last path segment
<IfModule mod_headers.c>
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} ([^/]+)/datasheet.pdf$
RewriteRule ^.*$ - [E=DATASHEETNAME:%1]
RequestHeader set datasheetname "%{DATASHEETNAME}e" env=DATASHEETNAME
Header set Content-Disposition "attachment; filename=%{HTTP_DATASHEETNAME}e.pdf" env=HTTP_DATASHEETNAME
</IfModule>
</IfModule>
@mhuber84
Copy link
Author

mhuber84 commented Feb 14, 2017

With this .htaccess rule, if the path to your pdf is "http://domain.tld/path/to/my/superduper/datasheet.pdf", the download will get the filename "superduper.pdf".

I work with Apache 2.4 and PHP 7.0 as fcgid. If the file is a static file on the disc (no php involved) or the header is set in the vhost configuration, then the workaround with the RequestHeader is not needed. The pdf is generated dynamically by TYPO3, but the "Header set" didn't work as soon as PHP was called. So it has nothing to do with TYPO3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment