Can't get mod rewrite rules working on IIS...

Hi, I'm trying to get the mod rewrite rules converted for IIS.  I used IIS's import rules tool and chose the htaccess htaccess_dist file (I assumed that was the right one).  After applying that, I got a 500 rules where it said:

HTTP Error 500.52 - URL Rewrite Module Error.
The expression "^pg\/([A-Za-z0-9\_\-]+)$" contains an unmatched '[' or ']'.

After digging around, I realized that it was because it wasn't supposed to escape the undscore on windows, so I changed it to say:

^pg\/([A-Za-z0-9_\-]+)$

This got rid of the 500 error, but despite the file being in my web.config, it still has this message in the install process:

We think your server is running the IIS web server.

The rewrite rules test failed. You need to configure your web server with Elgg's rewrite rules and try again.

I have no idea why the rewrite rules have failed at this point and there's no other information that it gives me.  Here's the entirety of my web.config file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Imported Rule 14" stopProcessing="true">
                    <match url="^pg\/([A-Za-z0-9_\-]+)$" ignoreCase="false" />
                    <action type="Rewrite" url="engine/handlers/page_handler.php?handler={R:1}&amp;{QUERY_STRING}" appendQueryString="false" />
                </rule>
                <rule name="Imported Rule 15" stopProcessing="true">
                    <match url="^pg\/([A-Za-z0-9_\-]+)\/(.*)$" ignoreCase="false" />
                    <action type="Rewrite" url="engine/handlers/page_handler.php?handler={R:1}&amp;page={R:2}&amp;{QUERY_STRING}" appendQueryString="false" />
                </rule>
                <rule name="Imported Rule 16" stopProcessing="true">
                    <match url="^tag\/(.+)\/?$" ignoreCase="false" />
                    <action type="Rewrite" url="engine/handlers/page_handler.php?handler=search&amp;page={R:1}" appendQueryString="false" />
                </rule>
                <rule name="Imported Rule 17" stopProcessing="true">
                    <match url="^action\/([A-Za-z0-9_\-\/]+)$" ignoreCase="false" />
                    <action type="Rewrite" url="engine/handlers/action_handler.php?action={R:1}&amp;{QUERY_STRING}" appendQueryString="false" />
                </rule>
                <rule name="Imported Rule 18" stopProcessing="true">
                    <match url="^cache\/(.*)$" ignoreCase="false" />
                    <action type="Rewrite" url="engine/handlers/cache_handler.php?request={R:1}&amp;{QUERY_STRING}" appendQueryString="false" />
                </rule>
                <rule name="Imported Rule 19" stopProcessing="true">
                    <match url="^services\/api\/([A-Za-z0-9_\-]+)\/(.*)$" ignoreCase="false" />
                    <action type="Rewrite" url="engine/handlers/service_handler.php?handler={R:1}&amp;request={R:2}&amp;{QUERY_STRING}" appendQueryString="false" />
                </rule>
                <rule name="Imported Rule 20" stopProcessing="true">
                    <match url="^export\/([A-Za-z]+)\/([0-9]+)\/?$" ignoreCase="false" />
                    <action type="Rewrite" url="engine/handlers/export_handler.php?view={R:1}&amp;guid={R:2}" appendQueryString="false" />
                </rule>
                <rule name="Imported Rule 21" stopProcessing="true">
                    <match url="^export\/([A-Za-z]+)\/([0-9]+)\/([A-Za-z]+)\/([A-Za-z0-9_]+)\/$" ignoreCase="false" />
                    <action type="Rewrite" url="engine/handlers/export_handler.php?view={R:1}&amp;guid={R:2}&amp;type={R:3}&amp;idname={R:4}" appendQueryString="false" />
                </rule>
                <rule name="Imported Rule 22" stopProcessing="true">
                    <match url="xml-rpc.php" ignoreCase="false" />
                    <action type="Rewrite" url="engine/handlers/xml-rpc_handler.php" />
                </rule>
                <rule name="Imported Rule 23" stopProcessing="true">
                    <match url="mt/mt-xmlrpc.cgi" ignoreCase="false" />
                    <action type="Rewrite" url="engine/handlers/xml-rpc_handler.php" />
                </rule>
                <rule name="Imported Rule 24" stopProcessing="true">
                    <match url="^rewrite.php$" ignoreCase="false" />
                    <action type="Rewrite" url="install.php" />
                </rule>
                <rule name="Imported Rule 25" stopProcessing="true">
                    <match url="^([A-Za-z0-9_\-]+)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="engine/handlers/page_handler.php?handler={R:1}" appendQueryString="true" />
                </rule>
                <rule name="Imported Rule 26" stopProcessing="true">
                    <match url="^([A-Za-z0-9_\-]+)\/(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="engine/handlers/page_handler.php?handler={R:1}&amp;page={R:2}" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
 Any idea why I'm still getting this error?
  • Hi Rodolfo,

    Correct :) you need the URL Re Write module for IIS in order for this to work. 

    I imported the htaccess_dist file (that comes as part of the Elgg 1.8 distribution) via the import rules in IIS, looked for the term \_ and removed the \ - The reason I believe is that IIS reads \_ in a different way to how Apache would read it, and therefore breaks it. Removing the \ fixes it. Sorry I can't be more specific than that.

    Elgg will work on IIS. I'm running IIS 7.5. I had problems getting it going, but it does work. PM me if you want a little more help with your article :)

  • Thanks, AmmyKami83, the config file was very helpful.  [Worked on 1.8.16]

  • I finally did get this working for me.

    Two things to note that helped me solve this:

    1. If you install elgg in its own folder underneath the root instead of directly in the web root (for example, instead of putting elgg in wwwroot/ you put it in wwwroot/elgg/), you will need to manually move the web.config file that is created in the wwwroot folder into the wwwroot/elgg/ folder.  This one took me forever to figure out.

    2. If the rules posted here no longer work, there's a good tutorial here to get a working web.config file: http://community.elgg.org/discussion/view/486696/elgg-on-iis-7

  • Installing Elgg 1.8.16 on windows 2008 r2 on local machine for dummies like me. The one in your basement or the one you are hidding from your parents. LOL

    1.Upload Elgg

    Make a folder on your root. Open Internet Information Services (IIS) Manager.

    If you are already running a site in the default server, make a new site and create a folder for it on the C:\Inetpub\wwwroot\yourfolder name. Open the new site and in the right plain open basic setting and add your folder name in the slot. If you don't know how to create the new site on your server that is beyond the scope of these instructions. So send me a message. I will help you.

    2. Find the zip file you downloaded and unzip it. Paste and copy the files inside the folder and paste them into the folder you created on your root.

    Now follow the permissions in the installation file from http://docs.elgg.org/wiki/Installation.

    Be sure to create the data folder and set permissions for it. It needs to be off your root so make a data folder in C:\Inetpub\data- or what ever you named the folder and be sure to set write permissions on it.

    Folow the instructions and make your database using phpmyadmin or what ever you use to create your databases with.

    Now for the tricky part. Let's get those rewrites correct before we try to install anything.

    Open htaccess_dist with some type of editor notepad+++ will work or if you want an easy one use Winsyntax 2.0. I just save the file back to my site folder as (saveas) .htaccess. Windows will not read it anyway.

    Go back to your site in Internet Information Services Manager open your site and look in the center plain for url rewrites. Open it. In the right plane you will see import rules. Open it. In the center plane you will see import rules at the top. Hit the square on the top right and find that .hataccess file you created and hit the import button. Be sure to hit the save button on the top right.  Rewrites will still not work, but we are getting there. By importing these rules the server created a file in your folder called web.php. Find it and open it with your editor. replace what is in it with what I post below and then go back to the http://docs.elgg.org/wiki/Installation and finish your installiton.

    3.web.php

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
    <system.webServer>
    <rewrite>
    <rules>
    <rule name="Imported Rule 1" stopProcessing="true">
    <match url="^pg\/([A-Za-z0-9_\-]+)$" ignoreCase="false" />
    <action type="Rewrite" url="engine/handlers/page_handler.php?handler={R:1}&amp;{QUERY_STRING}" appendQueryString="false" />
    </rule>
    <rule name="Imported Rule 2" stopProcessing="true">
    <match url="^pg\/([A-Za-z0-9_\-]+)\/(.*)$" ignoreCase="false" />
    <action type="Rewrite" url="engine/handlers/page_handler.php?handler={R:1}&amp;page={R:2}&amp;{QUERY_STRING}" appendQueryString="false" />
    </rule>
    <rule name="Imported Rule 3" stopProcessing="true">
    <match url="^tag\/(.+)\/?$" ignoreCase="false" />
    <action type="Rewrite" url="engine/handlers/page_handler.php?handler=search&amp;page={R:1}" appendQueryString="false" />
    </rule>
    <rule name="Imported Rule 4" stopProcessing="true">
    <match url="^action\/([A-Za-z0-9_\-\/]+)$" ignoreCase="false" />
    <action type="Rewrite" url="engine/handlers/action_handler.php?action={R:1}&amp;{QUERY_STRING}" appendQueryString="false" />
    </rule>
    <rule name="Imported Rule 5" stopProcessing="true">
    <match url="^cache\/(.*)$" ignoreCase="false" />
    <action type="Rewrite" url="engine/handlers/cache_handler.php?request={R:1}&amp;{QUERY_STRING}" appendQueryString="false" />
    </rule>
    <rule name="Imported Rule 6" stopProcessing="true">
    <match url="^services\/api\/([A-Za-z0-9_\-]+)\/(.*)$" ignoreCase="false" />
    <action type="Rewrite" url="engine/handlers/service_handler.php?handler={R:1}&amp;request={R:2}&amp;{QUERY_STRING}" appendQueryString="false" />
    </rule>
    <rule name="Imported Rule 7" stopProcessing="true">
    <match url="^export\/([A-Za-z]+)\/([0-9]+)\/?$" ignoreCase="false" />
    <action type="Rewrite" url="engine/handlers/export_handler.php?view={R:1}&amp;guid={R:2}" appendQueryString="false" />
    </rule>
    <rule name="Imported Rule 8" stopProcessing="true">
    <match url="^export\/([A-Za-z]+)\/([0-9]+)\/([A-Za-z]+)\/([A-Za-z0-9_]+)\/$" ignoreCase="false" />
    <action type="Rewrite" url="engine/handlers/export_handler.php?view={R:1}&amp;guid={R:2}&amp;type={R:3}&amp;idname={R:4}" appendQueryString="false" />
    </rule>
    <rule name="Imported Rule 9" stopProcessing="true">
    <match url="xml-rpc.php" ignoreCase="false" />
    <action type="Rewrite" url="engine/handlers/xml-rpc_handler.php" />
    </rule>
    <rule name="Imported Rule 10" stopProcessing="true">
    <match url="mt/mt-xmlrpc.cgi" ignoreCase="false" />
    <action type="Rewrite" url="engine/handlers/xml-rpc_handler.php" />
    </rule>
    <rule name="Imported Rule 11" stopProcessing="true">
    <match url="^rewrite.php$" ignoreCase="false" />
    <action type="Rewrite" url="install.php" />
    </rule>
    <rule name="Imported Rule 12" stopProcessing="true">
    <match url="^([A-Za-z0-9_\-]+)$" ignoreCase="false" />
    <conditions logicalGrouping="MatchAll">
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
    </conditions>
    <action type="Rewrite" url="engine/handlers/page_handler.php?handler={R:1}" appendQueryString="true" />
    </rule>
    <rule name="Imported Rule 13" stopProcessing="true">
    <match url="^([A-Za-z0-9_\-]+)\/(.*)$" ignoreCase="false" />
    <conditions logicalGrouping="MatchAll">
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
    </conditions>
    <action type="Rewrite" url="engine/handlers/page_handler.php?handler={R:1}&amp;page={R:2}" appendQueryString="true" />
    </rule>
    </rules>
    </rewrite>
    </system.webServer>
    </configuration>

    Ok Good luck and I hope this helps someone.