IIS7 Forcing Office Documents to Download w/o auth prompt

All the random crap I spend all day long figuring out.
Post Reply
User avatar
syntax
Site Admin
Posts: 54
Joined: Tue Jan 06, 2009 9:25 pm

IIS7 Forcing Office Documents to Download w/o auth prompt

Post by syntax » Wed Jan 22, 2014 3:41 pm

Idea from http://stackoverflow.com/questions/1940 ... ad-in-iis7
Which is from http://lruckman.wordpress.com/2012/09/2 ... -in-iis-7/
  1. Ensure you have enabled the URL Rewrite module for IIS7
  2. Open IIS7 Manager
  3. Navigate to the site (or sub site) you want to force downloads from
  4. In the Features View select Url Rewrite
  5. In the right hand column select "View Preconditions"
  6. Add the following precondition
  7. Name: FileIsMP4
  8. Using: Regular Expressions
  9. Logical Grouping: Match All
  10. Contition input: {REQUEST_FILENAME}
  11. Check if input string: Matches the pattern
  12. Pattern: \.mp4$
  13. Go back to the URL Rewrite list of rules and add the following outbound rule
  14. Name: Force Download For MP4s
  15. Precondition: FileIsMP4
  16. Match - Matching scope: Server Variable
  17. Match - Variable name: RESPONSE_Content-Disposition
  18. Match - Variable value: Matches the Pattern
  19. Match - Using: Regular Expressions
  20. Match - Pattern: .*
  21. Match - Ignore Case - yes
  22. Conditions - Logical grouping: Match All
  23. Conditions - Input: {REQUEST_FILENAME}
  24. Conditions - Type: Matches the Pattern
  25. Conditions - Pattern: (.*)\\([^/]+)\.mp4$
  26. Conditions - Track capture groups across conditions: yes
  27. Action - Action type: Rewrite
  28. Action - Action Properties - Value: attachment; filename={C:2}.mp4
  29. Action - Replace existing server variable value: yes
  30. Action - Stop processing of subsequent rules: no
End resulting web.config file:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <outboundRules>
				<rule name="Force DOC Download" preCondition="FileIsDOC">
                    <match serverVariable="RESPONSE_Content-Disposition" pattern=".*" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" pattern="(.*)\\([^/]+)\.doc$" />
                    </conditions>
                    <action type="Rewrite" value="attachment; filename="{C:2}.doc"" />
                </rule>
				<rule name="Force DOCX Download" preCondition="FileIsDOCX">
                    <match serverVariable="RESPONSE_Content-Disposition" pattern=".*" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" pattern="(.*)\\([^/]+)\.docx$" />
                    </conditions>
                    <action type="Rewrite" value="attachment; filename="{C:2}.docx"" />
                </rule>
				<rule name="Force PDF Download" preCondition="FileIsPDF" enabled="true">
                    <match serverVariable="RESPONSE_Content-Disposition" pattern=".*" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" pattern="(.*)\\([^/]+)\.pdf$" />
                    </conditions>
                    <action type="Rewrite" value="attachment; filename="{C:2}.pdf"" />
                </rule>
				<rule name="Force PPT Download" preCondition="FileIsPPT">
                    <match serverVariable="RESPONSE_Content-Disposition" pattern=".*" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" pattern="(.*)\\([^/]+)\.ppt$" />
                    </conditions>
                    <action type="Rewrite" value="attachment; filename="{C:2}.ppt"" />
                </rule>
				<rule name="Force PPTX Download" preCondition="FileIsPPTX">
                    <match serverVariable="RESPONSE_Content-Disposition" pattern=".*" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" pattern="(.*)\\([^/]+)\.pptx$" />
                    </conditions>
                    <action type="Rewrite" value="attachment; filename="{C:2}.pptx"" />
                </rule>
                <rule name="Force XLS Download" preCondition="FileIsXLS">
                    <match serverVariable="RESPONSE_Content-Disposition" pattern=".*" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" pattern="(.*)\\([^/]+)\.xls$" />
                    </conditions>
                    <action type="Rewrite" value="attachment; filename="{C:2}.xls"" />
                </rule>
				<rule name="Force XLSX Download" preCondition="FileIsXLSX">
                    <match serverVariable="RESPONSE_Content-Disposition" pattern=".*" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" pattern="(.*)\\([^/]+)\.xlsx$" />
                    </conditions>
                    <action type="Rewrite" value="attachment; filename="{C:2}.xlsx"" />
                </rule>
                <preConditions>
					<preCondition name="FileIsPDF">
                        <add input="{REQUEST_FILENAME}" pattern="\.pdf$" />
                    </preCondition>
					<preCondition name="FileIsPPT">
                        <add input="{REQUEST_FILENAME}" pattern="\.ppt$" />
                    </preCondition>
					<preCondition name="FileIsPPTX">
                        <add input="{REQUEST_FILENAME}" pattern="\.pptx$" />
                    </preCondition>
					<preCondition name="FileIsDOC">
                        <add input="{REQUEST_FILENAME}" pattern="\.doc$" />
                    </preCondition>
					<preCondition name="FileIsDOCX">
                        <add input="{REQUEST_FILENAME}" pattern="\.docx$" />
                    </preCondition>
                    <preCondition name="FileIsXLS">
                        <add input="{REQUEST_FILENAME}" pattern="\.xls$" />
                    </preCondition>
					<preCondition name="FileIsXLSX">
                        <add input="{REQUEST_FILENAME}" pattern="\.xlsx$" />
                    </preCondition>
                </preConditions>
            </outboundRules>
        </rewrite>
    </system.webServer>
</configuration>

User avatar
syntax
Site Admin
Posts: 54
Joined: Tue Jan 06, 2009 9:25 pm

Re: IIS7 Forcing Office Documents to Download w/o auth promp

Post by syntax » Wed Jan 29, 2014 5:48 pm

Later found out that file names with spaces were not being sent to the browser correctly. I had to put quotes around the filename rewrite parameter (e.g. filename="{C:2}.pdf"). Post above updated.

Post Reply