Drupal admin: clean URLs for Drupal using IIS

Submitted by Frederic Marand on

Using Apache, clean URLs in Drupal compatible with SEO are achieved using mod_rewrite, but no built-in equivalent seems to exist for Microsoft IIS. There is a hack, though...

First, there is a documented way of achieving clean URLs in Drupal, on the Drupal.org site, at http://drupal.org/node/3854.

However, it involves adding code into settings.php, which may not please everyone, and it works by relying on custom error pages in IIS, causing either lots of error to be recorded in the logs, or requiring the sysop to stop recording such errors and therefore miss on true error page.

There is another way: IIS allows modules to filter URLs prior to transmitting them to the script engine, and at least one solution exists at no cost to emulate the mod_rewrite behaviour: it is called ISAPI_Rewrite Lite.

As the name suggests, it is not free software, but comes at no cost, which can make it acceptable for development workstations. Commercial versions exist for the full version in case one would wish to run a production Drupal on IIS.

All it takes is installing the software by the usual click, click, click method, then editing the httpd.ini configuration file in the product directory (link supplied by the installer from the Start menu).

Supposing your (presumably local) Drupal instance on IIS is hosted on http://localhost/someroot/example, the following set of rules will make Drupal 4.6.3 workable with clean URLs:

RewriteRule (.*)/example/(.*)\.(?:css|jpg|gif|png) $0 [L]
RewriteRule (.*)/example/\?q=(.*) $0 [L]
RewriteRule (.*)/audean/index.php\?q=(.*) $0 [L]
RewriteRule (.*)/example/(.*) $1/example\?q=$2 [L]

Here is how it works:

  1. The first rule prevents the rewriting of files accessed directly, not going through Drupal
  2. The second rule prevents the rewriting of already rewritten paths
  3. The second rule prevents the rewriting of paths already rewritten and used in forms, which make index.php appear
  4. The fourth rule remaps URLs not blocked by the previous rules to the query format.

Obviously, this may need tuning on any given site, for instance by masking specific download directories or additional file type. Also, note that although the regex matching is very fast (a few microseconds) on a 2 GHz PC, this approach appears to have a significant impact on performance, because the rewriting does not directly return the page at the rewritten URL, but relies on a browser-based roundtrip to the URL using a 302 status code, which also causes the actual, query-based URL, to appear in the address bar although Drupal generated clean URLs.

To conclude, this is efficient on a developer's machine but, like the solution outlined on Drupal.org, is probably not to be advised on a production site.

UPDATE 09/09/05: I just noticed a more elaborate version of this technique using the pay-for version of ISAPI_Rewrite and a longer configuration file is also documented on drupal.org at http://drupal.org/node/27141.