Force MS document’s extension and MIME type through .htaccess

The problem

If you use MacOS and Chrome browser (recently this has been reported under other browsers and OS as well) you might have experienced an issue when downloading any Microsoft documents like Word, Excel, PoerPoint their file extension is automatically swapped to “.zip”. The reason for that are the server configurations and more specifically the MIME types of those files. Most browsers do not recognise the new “.docx”, “.xlsx”, “.pptx”. If you haven’t spotted that your users definitely have so it’d be good to test and apply that fix.

 

The solution

You can force the MIME type of those files in the .htaccess file which should be located in you public directory. To do so use the following code:
[cc escaped=”true” lang=”text”]AddType application/vnd.ms-word.document.macroEnabled.12 .docm
AddType application/vnd.openxmlformats-officedocument.wordprocessingml.document docx
AddType application/vnd.openxmlformats-officedocument.wordprocessingml.template dotx
AddType application/vnd.ms-powerpoint.template.macroEnabled.12 potm
AddType application/vnd.openxmlformats-officedocument.presentationml.template potx
AddType application/vnd.ms-powerpoint.addin.macroEnabled.12 ppam
AddType application/vnd.ms-powerpoint.slideshow.macroEnabled.12 ppsm
AddType application/vnd.openxmlformats-officedocument.presentationml.slideshow ppsx
AddType application/vnd.ms-powerpoint.presentation.macroEnabled.12 pptm
AddType application/vnd.openxmlformats-officedocument.presentationml.presentation pptx
AddType application/vnd.ms-excel.addin.macroEnabled.12 xlam
AddType application/vnd.ms-excel.sheet.binary.macroEnabled.12 xlsb
AddType application/vnd.ms-excel.sheet.macroEnabled.12 xlsm
AddType application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx
AddType application/vnd.ms-excel.template.macroEnabled.12 xltm
AddType application/vnd.openxmlformats-officedocument.spreadsheetml.template xltx[/cc]
Once you do that every time the server send a file to the user it will add the correct MIME type in the HTTP request and the file will be served and saved with ist original file extension.

Second fix

If you have a root access to your server you can add this fix directly to your “mime.types” file which should be located at: “/etc/mime.types”
[cc escaped=”true” lang=”text”]application/vnd.ms-word.document.macroEnabled.12 .docm
application/vnd.openxmlformats-officedocument.wordprocessingml.document docx
application/vnd.openxmlformats-officedocument.wordprocessingml.template dotx
application/vnd.ms-powerpoint.template.macroEnabled.12 potm
application/vnd.openxmlformats-officedocument.presentationml.template potx
application/vnd.ms-powerpoint.addin.macroEnabled.12 ppam
application/vnd.ms-powerpoint.slideshow.macroEnabled.12 ppsm
application/vnd.openxmlformats-officedocument.presentationml.slideshow ppsx
application/vnd.ms-powerpoint.presentation.macroEnabled.12 pptm
application/vnd.openxmlformats-officedocument.presentationml.presentation pptx
application/vnd.ms-excel.addin.macroEnabled.12 xlam
application/vnd.ms-excel.sheet.binary.macroEnabled.12 xlsb
application/vnd.ms-excel.sheet.macroEnabled.12 xlsm
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx
application/vnd.ms-excel.template.macroEnabled.12 xltm
application/vnd.openxmlformats-officedocument.spreadsheetml.template xltx[/cc]
For Apache 2.2 servers you might need to update the mime type configurations “conf/mime.types” as follows:

Old configurations
[cc escaped=”true” lang=”text”]application/msword doc dot
application/vnd.ms-excel xls xlm xla xlc xlt xlw
application/vnd.ms-powerpoint ppt pps pot potx sldx[/cc]
New configurations
[cc escaped=”true” lang=”text”]application/msword doc docx dot dotx
application/vnd.ms-excel xls xlsx xlm xla xlc xlt xltx xlw
application/vnd.ms-powerpoint ppt pptx pps ppsx pot potx potx sldx[/cc]

It's only fair to share...Buffer this pageShare on Facebook0Tweet about this on TwitterShare on Google+0Share on LinkedIn0Share on Reddit0Pin on Pinterest0Email this to someone
About

Just a guy with strong interest in PHP and Web technologies

Posted in Apache, Servers, Small Tips Tagged with: , , , , ,
One comment on “Force MS document’s extension and MIME type through .htaccess
  1. Marc says:

    Brilliant. Thanks for posting this here. Well written info to help browsers to behave properly with Office Docs.

Leave a Reply

Your email address will not be published. Required fields are marked *

*