Saotn.org

Convert Apache .htaccess to IIS web.config

Convert Apache .htaccess to IIS web.config


This post describes some of the IIS URL Rewrite equivalents of commonly used Apache/.htaccess settings. You may find this useful when you want to convert .htaccess to web.config for IIS 7.5 and 8.0 web.config. The second part of this series outlines how to use Internet Information Services (IIS) 7.0 Manager to import and convert a .htaccess file to web.config. You might also be interested in some .htaccess examples for IIS (to use with Helicon ISAPI_Rewrite or Ape).

Convert Apache .htaccess to IIS web.config

IIS 7.5 / 8.0 web.config equivalents for Apache .htaccess (mod_dir, mod_headers, mod_mime mod_rewrite and mod_gzip)

mod_dir

http://httpd.apache.org/docs/2.2/mod/mod_dir.html
Provides for “trailing slash” redirects and serving directory index files

IIS web.config: trailing directory slash

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <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>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

IIS web.config: default document -or directory index- files and their order

list of resources to look for when the client requests a directory

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <defaultDocument>
      <files>
        <!-- delete all currently configured index files -->
        <clear />
        <!-- add first index file -->
        <add value="index.php" />
        <!-- add second index file -->
        <add value="index.html" />
      </files>
    </defaultDocument>
  </system.webServer>
</configuration>

IIS web.config: enable and disable directory browsing

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <!-- set to false to disable -->
    <directoryBrowse enabled="true" />
  </system.webServer>
</configuration>

mod_headers

http://httpd.apache.org/docs/2.2/mod/mod_headers.html
Customization of HTTP request and response headers

IIS web.config: some example response headers

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <httpProtocol>
      <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>
    </httpProtocol>
  </system.webServer>
</configuration>

mod_mime

http://httpd.apache.org/docs/2.2/mod/mod_mime.html
Associates the requested filename’s extensions with the file’s behavior (handlers and filters) and content (mime-type, language, character set and encoding)

IIS web.config: set MIME-type and charset for file extensions

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <httpProtocol>
      <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; charset=UTF-8" />
        -->
        <remove fileExtension=".css" />
        <mimeMap fileExtension=".css" mimeType="text/css" />
        <remove fileExtension=".js" />
        <mimeMap fileExtension=".js" mimeType="text/javascript; charset=UTF-8" />
      </staticContent>
    </httpProtocol>
  </system.webServer>
</configuration>

mod_rewrite

http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
Provides a rule-based rewriting engine to rewrite requested URLs on the fly

IIS web.config: equivalent (most commenly used variant)

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="wordpress" patternSyntax="Wildcard">
	  <match url="*" />
	    <conditions>
	      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
	      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
	    </conditions>
	    <action type="Rewrite" url="index.php" />
	</rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

mod_deflate

http://httpd.apache.org/docs/2.2/mod/mod_deflate.html
Compress content before it is delivered to the client

IIS web.config: gzip compression (not deflate), for both static and dynamic file types

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <!-- 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>
  </system.webServer>
</configuration>

Compression with gzip is mostly configured at the webserver level

Freebies, web.config examples

You can configure a lot of other neat stuff in your web.config file. Three (3) examples.

freebie #1: Set Expire headers to 30 days for static content

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <httpProtocol>
      <staticContent>
        <!-- Set expire headers to 30 days for static content -->
        <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" />
      </staticContent>
    </httpProtocol>
  </system.webServer>
</configuration>

freebie #2: remove/disable unused handlers


 

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <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>
  </system.webServer>
</configuration>

freebie #3: remove/disable unused modules, or add them

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <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>

Import .htaccess file with Internet Information Services (IIS) 7.0 Manager and convert to web.config

In the second part of this post series you can learn how to use Internet Information Services (IIS) 7.0 Manager to import and convert a .htaccess file to web.config:

IIS Outbound Rewrite Rules

Also see for more information on IIS Outbound Rules:

 

 

 


Sysadmins of the North
About The Author
My name is Jan Reilink. I am not a hacker, coder, developer, programmer or guru. I am merely a system administrator, doing his daily thing at Vevida Services in the Netherlands. With over 10 years of experience, my specialties include Windows Server (2003, 2008 and 2012), Windows 7, IIS (6.0, 7.5 and 8.0), Linux (CentOS, Debian), PHP, websites, optimization and security.

 

1 Comment:



Leave a Reply




2 + = 8

About Sysadmins of the North

Hi and welcome to Sysadmins of the North!

Sysadmins of the North is just another technical blog. Just like so many others out there. Most posts are written in English, some in Dutch. On Saotn.org you can find all kinds of computer, server, web, sysadmin, database and security related stuff.

About me: My name is Jan Reilink. I am not a hacker, coder, developer, programmer or guru. I am merely a system administrator, doing his daily thing at Vevdia Services in the Netherlands. Living in the north of the Netherlands, so hence the name Sysadmins of the North :-)

Drop me a comment somewhere or send an email to say hi, or discuss about security, website or WordPress, performance, Windows or IIS topics.

Search & Find

Advertisement

IT Books & WP Themes

Windows PowerShell Cookbook: The Complete Guide to Scripting Microsoft\'s Command Shell
DNS and BIND - 5th Edition
DNSSEC Mastery: Securing the Domain Name System with BIND
Windows Server 2012 Unleashed
Enfold - Responsive Multi-Purpose WordPress Theme
Striking MultiFlex & Ecommerce Responsive WordPress Theme

 

The Sysadmins of the North network

Just for the fun of it, Sysadmins of the North is hosted on mulitple servers:

  1. one (shared) Windows Server 2012, IIS 8.0 webserver running PHP 5.5
  2. one (shared) MySQL database server, running MariaDB 5.5
  3. one Varnish Cache HTTP reverse proxy with Apache, for offloaded static content
Twitter Feed

What's happening, right now, around Saotn.org?


Bad Authentication data
Copyright © 2007-2014 Saotn.org . Design by OrangeIdea