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/ .docm
AddType application/vnd.openxmlformats-officedocument.wordprocessingml.document docx
AddType application/vnd.openxmlformats-officedocument.wordprocessingml.template dotx
AddType application/ potm
AddType application/vnd.openxmlformats-officedocument.presentationml.template potx
AddType application/ ppam
AddType application/ ppsm
AddType application/vnd.openxmlformats-officedocument.presentationml.slideshow ppsx
AddType application/ pptm
AddType application/vnd.openxmlformats-officedocument.presentationml.presentation pptx
AddType application/ xlam
AddType application/ xlsb
AddType application/ xlsm
AddType application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx
AddType application/ 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/ .docm
application/vnd.openxmlformats-officedocument.wordprocessingml.document docx
application/vnd.openxmlformats-officedocument.wordprocessingml.template dotx
application/ potm
application/vnd.openxmlformats-officedocument.presentationml.template potx
application/ ppam
application/ ppsm
application/vnd.openxmlformats-officedocument.presentationml.slideshow ppsx
application/ pptm
application/vnd.openxmlformats-officedocument.presentationml.presentation pptx
application/ xlam
application/ xlsb
application/ xlsm
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx
application/ 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/ xls xlm xla xlc xlt xlw
application/ ppt pps pot potx sldx[/cc]
New configurations
[cc escaped=”true” lang=”text”]application/msword doc docx dot dotx
application/ xls xlsx xlm xla xlc xlt xltx xlw
application/ ppt pptx pps ppsx pot potx potx sldx[/cc]


Just a guy with strong interest in PHP and Web technologies

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 to Marc Cancel reply

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


This site uses Akismet to reduce spam. Learn how your comment data is processed.