Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 514
Steph Avatar asked Mar 31 '09 18:03

Steph


2 Answers

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.

like image 131
ojrac Avatar answered Oct 08 '22 09:10

ojrac


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>

like image 28
olle Avatar answered Oct 08 '22 09:10

olle