Skip to content

Instantly share code, notes, and snippets.

@rcnascimento
Last active April 23, 2019 15:01
Show Gist options
  • Save rcnascimento/9147136 to your computer and use it in GitHub Desktop.
Save rcnascimento/9147136 to your computer and use it in GitHub Desktop.
Generic configuration file for IIS (+ WordPress rules)
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<!--
Default file - or directory index - files and their order
Source: Convert Apache .htaccess to IIS web.config <http://www.saotn.org/convert-apache-htaccess-iis-web-config/>
-->
<defaultDocument>
<files>
<!-- delete all currently configured index files -->
<clear />
<add value="index.php" />
</files>
</defaultDocument>
<!--
Disable directory listing
Source: Convert Apache .htaccess to IIS web.config <http://www.saotn.org/convert-apache-htaccess-iis-web-config/>
-->
<directoryBrowse enabled="false" />
<rewrite>
<rules>
<!--
Remove www from url
Source: Redirect to non-www-url domain with ASP.NET? <http://stackoverflow.com/questions/3328986/redirect-to-non-www-url-domain-with-asp-net>
-->
<rule name="Remove WWW prefix" >
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.com" />
</conditions>
<action type="Redirect" url="http://example.com/{R:1}" redirectType="Permanent" />
</rule>
<!--
Remove the index.php from url
-->
<rule name="Remove directory index">
<match url="index.php(.*)$" />
<action type="Redirect" url="/{R:1}" redirectType="Permanent" />
</rule>
<!--
Trailing directory slash
Source: Convert Apache .htaccess to IIS web.config <http://www.saotn.org/convert-apache-htaccess-iis-web-config/>
Note: The order is important if used with WordPress. Must come before.
-->
<rule name="Add trailing slash" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<!-- if no file by that name exists -->
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<!-- if a directory by that name does exist -->
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="false" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule>
<!--
Default Wordpress rewrite rules
Source: Enabling Pretty Permalinks in Wordpress <http://www.iis.net/learn/extensions/url-rewrite-module/enabling-pretty-permalinks-in-wordpress>
-->
<rule name="WordPress Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
<!--
Rewrite the gzip compressed CSS and JS files
Source: Convert Apache .htaccess to IIS web.config, Part 2: IIS Manager import <http://www.saotn.org/convert-apache-htaccess-iis-web-config-part2-iis-manager-import/>
-->
<rule name="GZIP_RewriteRule" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_METHOD}" pattern="POST" ignoreCase="false" negate="true" />
<add input="{HTTP_ACCEPT_ENCODING}" pattern="gzip" ignoreCase="false" />
<add input="{REQUEST_FILENAME}.gz" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:0}.gz" />
</rule>
</rules>
</rewrite>
<httpProtocol>
<!--
Some example response headers
Source: Convert Apache .htaccess to IIS web.config <http://www.saotn.org/convert-apache-htaccess-iis-web-config/>
-->
<customHeaders>
<remove name="ETag"/>
<!-- Set a Access-Control-Allow-Origin header -->
<add name="Access-Control-Allow-Origin" value="*"/>
<!-- Set a X-UA-Compatible header -->
<add name="X-UA-Compatible" value="IE=Edge,chrome=1"/>
<!-- remove the X-Powered-By header -->
<remove name="X-Powered-By"/>
<!-- Set a Cache-Control header with max-age=691200 value -->
<add name="Cache-Control" value="max-age=691200" />
</customHeaders>
<!--
Set MIME-type and charset for file extensions
Source: Convert Apache .htaccess to IIS web.config <http://www.saotn.org/convert-apache-htaccess-iis-web-config/>
-->
<staticContent>
<remove fileExtension=".html" />
<mimeMap fileExtension=".html" mimeType="text/html; charset=UTF-8" />
<remove fileExtension=".htm" />
<mimeMap fileExtension=".htm" mimeType="text/html; charset=UTF-8" />
<!-- DON'T set UTF-8 for .css, it breaks markup in Internet Explorer -->
<remove fileExtension=".css" />
<mimeMap fileExtension=".css" mimeType="text/css" />
<remove fileExtension=".js" />
<mimeMap fileExtension=".js" mimeType="text/javascript; charset=UTF-8" />
<!--
Set Expire headers to 30 days for static content
Source: Convert Apache .htaccess to IIS web.config <http://www.saotn.org/convert-apache-htaccess-iis-web-config/>
-->
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" />
</staticContent>
</httpProtocol>
<!--
Gzip compression (not deflate), for both static and dynamic file types
Source: Convert Apache .htaccess to IIS web.config <http://www.saotn.org/convert-apache-htaccess-iis-web-config/>
-->
<!-- set minFileSizeForComp to 256 kilobytes, the minimum size to allow compression -->
<httpCompression minFileSizeForComp="256">
<scheme name="gzip" dll="%Windir%system32inetsrvgzip.dll" />
<dynamicTypes>
<clear />
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<!-- text/javascript MUST BE the same as in the mimeMap -->
<add mimeType="text/javascript" enabled="true" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>
<staticTypes>
<clear />
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<!-- text/javascript MUST BE the same as in the mimeMap -->
<add mimeType="text/javascript" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
</httpCompression>
<!--
Remove/disable unused handlers
Source: Convert Apache .htaccess to IIS web.config <http://www.saotn.org/convert-apache-htaccess-iis-web-config/>
-->
<handlers>
<!-- remove Perl (.cgi, .pl) if unused -->
<remove name="PerlPLX" />
<!-- remove PHP3 (.php3) if unused -->
<remove name="PHP3" />
<!-- remove PHP4 (.php4) if unused -->
<remove name="PHP4" />
<!-- remove ISAPI_RewriteProxy 64-bit if unused -->
<remove name="ISAPI_RewriteProxy-64" />
<!-- remove ISAPI_RewriteProxy if unused -->
<remove name="ISAPI_RewriteProxy" />
<!-- remove PHP (.php) if unused -->
<remove name="PHP" />
</handlers>
<!--
Remove/disable unused modules, or add them
Source: Convert Apache .htaccess to IIS web.config <http://www.saotn.org/convert-apache-htaccess-iis-web-config/>
-->
<modules>
<!-- remove Helicontech APE -->
<remove name="Helicon.Ape" />
<!-- Add Uri- File- and Token cache modules -->
<!-- for IIS Output Cache -->
<add name="UriCacheModule" />
<add name="FileCacheModule" />
<add name="TokenCacheModule" />
</modules>
</system.webServer>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment