Apache .htaccess Entry For Serving Correct MimeType For Some Of The More Common File-Types

Put this part in your .htaccess to make sure your files are served with correct mimetype, and will eventually trigger the target-browser/OS, proper, handler.

<IfModule mod_mime.c> 
  AddType application/font-woff                  woff
  AddType application/javascript                 js
  AddType application/octet-stream               safariextz
  AddType application/vnd.ms-fontobject          eot
  AddType application/x-chrome-extension         crx
  AddType application/x-font-ttf                 ttf ttc
  AddType application/x-font-woff                woff
  AddType application/x-mpegurl                  m3u8
  AddType application/x-xpinstall                xpi
  AddType audio/mp4                              m4a
  AddType audio/mpeg                             mp3
  AddType audio/ogg                              oga ogg
  AddType font/opentype                          otf
  AddType image/webp                             webp
  AddType image/x-icon                           ico
  AddType text/cache-manifest                    appcache manifest
  AddType text/plain                             srt sub ssa
  AddType text/vtt                               vtt
  AddType text/x-component                       htc
  AddType text/x-vcard                           vcf
  AddType video/3gpp                             3gp
  AddType video/mp2t                             ts
  AddType video/mp4                              mp4 m4v
  AddType video/ogg                              ogv ogg
  AddType video/quicktime                        mov
  AddType video/x-ms-wmv                         wmv
  AddType video/webm                             webm
  AddType video/x-flv                            flv
  AddType video/x-msvideo                        avi

  # Web application-manifests.  [https://github.com/mdn/to-do-notifications/blob/gh-pages/.htaccess]
  AddType application/x-web-app-manifest+json    webapp

  # SVG.
  #   Required for svg webfonts on iPad [twitter.com/FontSquirrel/status/14855840545]
  AddType image/svg+xml                          svg svgz 
  AddEncoding gzip                               svgz
</IfModule>

HTML Proxy Configuration For Apache

Essentially, it tries to locate “links” (URL, URI, whateva’…) in (X)HTML Documents,
kindd’a smart..

The following is a pin-point location on “Where Would \ONE\ Would Find A Link”… :)

# Configuration example.
#
# For detailed information about these directives see
# <URL:http://httpd.apache.org/docs/2.4/mod/mod_proxy_html.html>
# and for mod_xml2enc see
# <URL:http://httpd.apache.org/docs/2.4/mod/mod_xml2enc.html>
#
# First, to load the module with its prerequisites.  Note: mod_xml2enc
# is not always necessary, but without it mod_proxy_html is likely to
# mangle pages in encodings other than ASCII or Unicode (utf-8).
#
# For Unix-family systems:
# LoadFile	/usr/lib/libxml2.so
# LoadModule	proxy_html_module	modules/mod_proxy_html.so
# LoadModule	xml2enc_module		modules/mod_xml2enc.so
#
# For Windows (I don't know if there's a standard path for the libraries)
# LoadFile	C:/path/zlib.dll
# LoadFile	C:/path/iconv.dll
# LoadFile	C:/path/libxml2.dll
# LoadModule	proxy_html_module	modules/mod_proxy_html.so
# LoadModule	xml2enc_module		modules/mod_xml2enc.so
# 
# All knowledge of HTML links has been removed from the mod_proxy_html
# code itself, and is instead read from httpd.conf (or included file)
# at server startup.  So you MUST declare it.  This will normally be
# at top level, but can also be used in a <Location>.
#
# Here's the declaration for W3C HTML 4.01 and XHTML 1.0

ProxyHTMLLinks	a		href
ProxyHTMLLinks	area		href
ProxyHTMLLinks	link		href
ProxyHTMLLinks	img		src longdesc usemap
ProxyHTMLLinks	object		classid codebase data usemap
ProxyHTMLLinks	q		cite
ProxyHTMLLinks	blockquote	cite
ProxyHTMLLinks	ins		cite
ProxyHTMLLinks	del		cite
ProxyHTMLLinks	form		action
ProxyHTMLLinks	input		src usemap
ProxyHTMLLinks	head		profile
ProxyHTMLLinks	base		href
ProxyHTMLLinks	script		src for

# To support scripting events (with ProxyHTMLExtended On),
# you'll need to declare them too.

ProxyHTMLEvents	onclick ondblclick onmousedown onmouseup \
		onmouseover onmousemove onmouseout onkeypress \
		onkeydown onkeyup onfocus onblur onload \
		onunload onsubmit onreset onselect onchange

# If you need to support legacy (pre-1998, aka "transitional") HTML or XHTML,
# you'll need to uncomment the following deprecated link attributes.
# Note that these are enabled in earlier mod_proxy_html versions
#
# ProxyHTMLLinks	frame		src longdesc
# ProxyHTMLLinks	iframe		src longdesc
# ProxyHTMLLinks	body		background
# ProxyHTMLLinks	applet		codebase
#
# If you're dealing with proprietary HTML variants,
# declare your own URL attributes here as required.
#
# ProxyHTMLLinks	myelement	myattr otherattr
#
###########
# EXAMPLE #
###########
#
# To define the URL /my-gateway/ as a gateway to an appserver with address
# http://some.app.intranet/ on a private network, after loading the
# modules and including this configuration file:
#
# ProxyRequests Off  <-- this is an important security setting
# ProxyPass /my-gateway/ http://some.app.intranet/
# <Location /my-gateway/>
#	ProxyPassReverse /
#	ProxyHTMLEnable On
#	ProxyHTMLURLMap http://some.app.intranet/ /my-gateway/
#	ProxyHTMLURLMap / /my-gateway/
# </Location>
#
# Many (though not all) real-life setups are more complex.
#
# See the documentation at
# http://apache.webthing.com/mod_proxy_html/
# and the tutorial at
# http://www.apachetutor.org/admin/reverseproxies

Content-Disposition Header – Like A Boss 👔☕

##place inside a folder that contains some music videos and documents, this will override the default handler.

<FilesMatch "\.(mov|mp3|pdf)$">

  <IfModule mod_mime.c> 
    ForceType application/octet-stream
  </IfModule>

  <IfModule mod_headers.c> 
    ##fix a bug in old GoDaddy servers.
    Header unset X-Content-Type-Options
    Header unset Content-Disposition

    ##prevent mimetype sniffing (first few bytes can determine that a file should be opened in browser).
    Header set X-Content-Type-Options "nosniff"

    ##extract filename, apply to proper (for example) Content-Disposition: attachment; file="my music.mp3"
    SetEnvIf Request_URI "^.*/([^/]*)$" FILENAME=$1
    Header set "Content-Disposition" "attachment;filename=\"%{FILENAME}e\""
    UnsetEnv FILENAME
  </IfModule>

</FilesMatch>

forcing type / because of ‘reasons’? 👐
If this header is used in a response with the application/octet- stream content-type, the implied suggestion is that the user agent should not display the response, but directly enter a `save response as…’ dialog.


also edit your HTML so links would use HTML5’s download attribute:

<a href="mydirectory/my%20music.mp3" download="my music.mp3" type="application/octet-stream" title="click to download">hello!!</a>

(which is good enough for Google).

✨And, naturally.. 👀 you can combine both of the solutions!

reference: W3 / Protocols – RFC2616 (Section 19).

also answered on stackoverflow

PHP Snippet – Proper UTF-8 And GZip With Buffers-Handling And Content-Length Header And Ratio Information

  1. first, set up internal engine to UTF-8.

    mb_language("uni");
    mb_internal_encoding("UTF-8");
    mb_http_input("UTF-8");
    mb_http_output("UTF-8");
    mb_regex_encoding("UTF-8");
    setlocale(LC_ALL, "he_IL.UTF-8");
    
    1. start the GZIP buffer
    2. start the internal-engine-buffer (now set for UTF-8).
    3. notice that the implicit flush is set twice, keep it.
    ob_start("ob_gzhandler");
    ob_implicit_flush(false);
    
    ob_start("mb_output_handler");
    ob_implicit_flush(false);
    
  2. use your PHP script as usual,
    echo content, print, print_r, var_dump, etc..

    for example:

    echo str_repeat("-=á黪ğřƳȪȭDZȌȢɸʌΈΣϣொᆕᶙṕℳℱⅧ⅜⇦∬⓲┲▶➂ⶴは㌵㠩דוזחטש", 3000);
    
  3. script end:
    we will close the buffers and flush them, in a backward-order
    ,

    this will enable us to collect data on the each of the output’s-length too!
    (I found it so interesting I will actually write some extra headers with the information..)

    1. (optionally)store size-of the raw output.
    2. close and flush the raw output.
    3. (optionally)store size-of the mb (UTF-8) buffered output.
    4. close and flush the mb_outputhandler (UTF-8) buffered output.
    5. (optionally)store size-of the mb (UTF-8) buffered output.
    6. close and flush the ob_gzhandler (GZIP) buffered output.
    $content_length__raw = ob_get_length();
    ob_end_flush(); //close the raw output which will be dumped into the internal engine buffer.
    
    $content_length__utf8 = ob_get_length();
    ob_end_flush(); //close the internal-engine buffer, which will be dumped into the gzip engine buffer.
    
    $content_length__gzip = ob_get_length();
    
  4. at this point the value of content_length__gzip is actually the Content-Length header value since its the real output length (the final layer).

  5. write the Content-Length header.

    header('Content-Length: ' . $content_length__gzip);
    
  6. any additional headers should be written now.
    for example:

    header('Content-Type: text/html; charset=utf-8');
    

  • the easiest way is to wrap it all in few functions..
    here is a complete example, that will write an additional interesting information as headers (why headers? well, naturally, altering with the body of the response will change the measurements.. so headers is the best way..)
  • the result will look like so:

    2014-08-02_181624

    2014-08-02_181757