Translating an Apache .htaccess file to an IIS web.config

14,154

Solution 1

This could be seen as cheating, but we use ISAPI_Rewrite, which lets you just use the .htaccess file for IIS. If you can get them to put it on the server, you won't need to translate anything.

Solution 2

Please be aware that this will only work on IIS7 and not on IIS6. Also this requires FastCGI to be setup and the URL Rewriting module to be installed and enabled. These are things your hoster will be able to verify for you. If all of the above is true then the following file should do the trick ( you might need to tweak the paths but again I think your hoster will be able to do this for you if you supply them with this example file.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <configSections>
        <sectionGroup name="system.webServer">
            <sectionGroup name="rewrite">
                <section name="rewriteMaps" overrideModeDefault="Allow" />
                <section name="rules" overrideModeDefault="Allow" />
        </sectionGroup>
    </sectionGroup>
</configSections>

<system.webServer>
    <!-- Mapping the .do extension to the PHP ISAPI module -->
    <handlers>
        <!-- the following line is very specific to your host
             please check the module name and the scriptProcessor 
             path with the system administrator! basically this is 
             the same as
             http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis-70/#EnableFastCGI
             only in .config format. -->
        <add name="MaskDoAsPHP" path=".do" verb="GET,HEAD,POST,DEBUG" modules="FastCgiModule" scriptProcessor="C:\PHP\php-cgi.exe" />
    </handlers>

    <!-- Setting the default handler. -->
    <defaultDocument>
        <files>
            <clear />
            <add value="home.do" />
        </files>
    </defaultDocument>

    <rewrite>
        <rules>
            <rule name="Removing do extension" stopProcessing="true">
                <match url="^(.*)$" ignoreCase="false" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                </conditions>
                <action type="Rewrite" url="{R1}.do" appendQueryString="true" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

Solution 3

IIS7 and above can import Apache .htaccess rules using the URL Rewrite module.

  1. Install the URL Rewrite module via the Microsoft Web Platform Installer
  2. Start IIS Manager and on the left, in the Connections pane, select your required site (e.g. Default Web Site)
  3. In the centre (Features View) double click URL Rewrite.
  4. In the right panel click Import Rules... then paste your rules from the .htaccess file into the Rewrite rules box
  5. Click apply in the right column.
Share:
14,154
Steph
Author by

Steph

Updated on June 07, 2022

Comments

  • Steph
    Steph almost 2 years

    I developed an application on my local using PHP, MySQL and Apache and it has a .htaccess file containing this:

    #Setting the default handler.
      DirectoryIndex home.do
    
    <IfModule mod_mime.c>
      #Supporting .do extensions    
         AddType application/x-httpd-php .do
    </IfModule>
    
    <IfModule mod_rewrite.c>
      #Removing .do file extension if necessary
         RewriteEngine on
         RewriteCond %{REQUEST_FILENAME} !-d
         RewriteCond %{REQUEST_FILENAME}\.do -f
         RewriteRule ^(.*)$ $1.do
    </IfModule>
    

    But I informed that my customer's web server is IIS and I have to use a web.config file instead of .htaccess. Can anyone direct me through this, please?

  • Steph
    Steph about 15 years
    thanks olle and im sorry for the late reply! I'm done finally, your code helped :D
  • olle
    olle about 15 years
    Ok good, if you let me know what was "wrong" with it I can update the awnser for future visitors.