To get Drupal's Clean URL's working for you the first thing you'll need to do is head over to the Helicon Tech website and get yourself a copy of their ISAPI Rewrite product. This comes in two flavors, ISAPI Rewrite Full and ISAPI Rewrite Free. And as you may have guessed the later does not cost you a cent, but comes with some limitations over the full version. The main limitation of the free version is that it does not support site level configuration files, which if you are only running a handful of site or are running Windows XP Pro with IIS 5.1 (which can only host a single site) is perfectly fine. I have a couple of dozen sites on my server so I elected to shell out the hard earned cash for the full version.
UPDATE: This guide is for ISAPI Rewrite 2.x only! See this guide for instructions for use with ISAPI Rewrite 3.x.
Before we go on I thought I better make note that there are some differences between mod_rewrite for Apache and ISAPI Rewrite for IIS. In relation to Drupal's Clean URL feature the pertinent difference is that the mod_rewrite rules included with Drupal rely on mod_rewrites directory and file existence checking which is a feature that ISAPI Rewrite does not support. This means that you must explicitly lay down the rules for ISAPI Rewrite which makes things a little more complex, but still very doable.
Anyway, on with the show. Broken down there are three basic steps for getting Clean URL's on IIS with ISAPI Rewrite, and they are;
Step 1 - Installing and configuring ISAPI Rewrite
Download either the full or the free version of ISAPI Rewrite from the Helicon Tech website and install it as you would any normal Windows program. If you are using Windows 2003 Server with IIS 6 you will need to add some permissions manually as the IIS_WPG group willl need at least NTFS read permissions to all ISAPI_Rewrite dlls, rwhelper.dll.manifest and all httpd.ini files (modify permissions recommended on the directories containing httpd.ini files). Also if the proxy will be used (full version only) IIS_WPG should be given Modify permissions on the System temporary folder (usually C:\Windows\Temp).
Step 2 - Inserting your Drupal rewrite rules
If you are using the free version find the global httpd.ini file and open it, if you have the full version copy the global file and copy it to the root of your website. Once you have this done copy and paste the following code into the bottom of you httpd.ini file;
# Accept a url with the following directories and pass them through unchanged.
RewriteRule /(?:misc|files|modules|themes|sites|uploads)/(.*) $0 [I,L]
# Make URLs sane
RewriteRule /cron\.php $0 [I,L]
RewriteRule /index\.php.* $0 [I,L]
RewriteRule /update\.php.* $0 [I,L]
RewriteRule /xmlrpc\.php $0 [I,L]
# deactivate following line if "robotstxt" module is installed
#RewriteRule /robots\.txt.* $0 [I,L]
RewriteRule /(.*)\?(.*) /index.php\?q=$1&$2 [I,L]
RewriteRule /(.*) /index.php\?q=$1 [I,L] If your Drupal installation is in a subdirectory of your webroot, then you'll need to use the following rules;
# You must change/remove prefixes if Drupal is not installed in a subdirectory
# Specify namespaces with UriMatchPrefix - DO NOT CHANGE ORDER
# Alter the "/drupal" string below to the subdirectory that contains your Drupal install
UriMatchPrefix /drupal
# Accept a url with the following directories and pass them through unchanged.
RewriteRule /(?:misc|files|modules|themes|sites|uploads)/(.*) $0 [I,L]
# Make URLs sane
RewriteRule /cron\.php $0 [I,L]
RewriteRule /index\.php.* $0 [I,L]
RewriteRule /update\.php.* $0 [I,L]
RewriteRule /xmlrpc\.php $0 [I,L]
# deactivate following line if "robotstxt" module is installed
#RewriteRule /robots\.txt.* $0 [I,L]
# specify namespaces with UriFormatPrefix - DO NOT CHANGE SUXXX ORDER
UriFormatPrefix /drupal
RewriteRule /(.*)\?(.*) /index.php\?q=$1&$2 [I,L]
RewriteRule /(.*) /index.php\?q=$1 [I,L]
# reset namespaces to default
UriMatchPrefix
UriFormatPrefixNote: ISAPI Rewrite version 2.9 Build 63 or above is required for the above rule!
If you are using the free version and are using IIS 5 or 6 with multiple other sites that you don't wish to have the rewrite rules active on, then you'll need to specify which hostnames to exclude using the RewriteCond Host syntax. In the example below I excluded the iis-aid.com domains with the prefix of www, download and admin. All other hostnames will be rewritten.
RewriteCond Host: (?:www|download|admin|)\iis-aid\.comStep 3 - Configuring settings.php
The final step is to tell Drupal to turn on Clean URL's. This is a little more complicated than it sounds as all Apache users have to do is run the Clean URL test from within their Drupal admin console and all going well they are away as soon as they select the "enable" radio button. For IIS / ISAPI Rewrite users on the other hand you must manually override the Clean URL test as even though the Clean URL test is passed the "enable" button is still not selectable. However, by setting the following directive at the bottom of the settings.php file for your site we can override this;
$conf['clean_url'] = 1; // 1 enables, 0 clears clean_url That should be about it. You should now have Drupal Clean URL's up and running on your IIS system using ISAPI Rewrite. Please note that while I use these very same rules myself, I do not guarantee them in any shape or form. If you have Drupal installed in a sub directory then you will need to alter the httpd.ini rules to suit. Special thanks to Hass from the Drupal forums for helping me clean up these rules. If you have any issue or comments please feel free to leave your thoughts behind.
Comments
favicon.ico
What rule should be added to avoid page not found error with this location:
http://www.domain.com/index.php?q=favicon.ico
That's a bit o a weird one.
That's a bit o a weird one. What you could do is tell ISAPI Rewrite specifically not to rewrite anything with particular extensions. Here is a rule with several extensions you are not going to want to rewrite (add it under the first Drupal rewrite rule);
Alternative install for hosted IIS?
Is there an alternative installation (other than installer) to get this to work on a hosted IIS?
yes, I believe you can use
yes, I believe you can use the manual installation technique in hosted situations.
Question, getting a slight Problem
Excellent guide, question thought can't seem to get this to work:
So I set the sub-directory to /drupal/drupal-5.1
I have done the permessions and added the httpds.ini folder to the root of the domain that drupal is on. Added the sub-directory text I found here.
Enabled the rewrite by editing the config.
When I go to Administer it takes me to:
drupal/drupal-5.1/admin
So I was happy thinking it is all working...but now the page returns a 404 :(
I have to manually go to index.php?q=admin and none of my urls are working anymore have to enter them the same way. Any idea why this is so?
Thank you, appreciate anyones help.
Do you have access to your
Do you have access to your log files? If so, could you post the URI Stem for any of the URL's you are getting a 404 on?
/files directory failing
Hello, I have the full (trial) version installed on w2k3 server, and your guide was very helpful -especially- for a newbie like me.
Drupal is running nicely with clean urls. Only thing is that Drupal is not accesing data in the File folder.
Path to myweb:
C:\inetpub\vhosts\mydomain\httpdocs\mysitedir
Home dir in iis: http://xx.xx.xx.xx/mysitedir
files dir:
C:\inietpub\vhosts\mydomain\httpdocs\mysitedir\files
url to files:
http://xx.xx.xx.xx./files
Can u help?
Thanks
Nice tutorial ..however
when I finished step 3, I could no longer access my site, I was getting 404's on all page, except for my index.php.
See snippet from my error log:
2007-08-08 03:43:41 W3SVC670640477 216.98.148.xxx GET /index.php - 80 - 67.180.32.226 Mozilla/5.0+(Windows;+U;+Windows+NT+5.1;+en-US;+rv:1.8.1.6)+Gecko/20070725+Firefox/2.0.0.6 200 0 0
2007-08-08 03:43:47 W3SVC670640477 216.98.148.xxx GET /rss.xml - 80 - 67.180.32.226 Mozilla/5.0+(Windows;+U;+Windows+NT+5.1;+en-US;+rv:1.8.1.6)+Gecko/20070725+Firefox/2.0.0.6 404 0 2
2007-08-08 03:43:53 W3SVC670640477 216.98.148.xxx GET /node/add - 80 - 67.180.32.226 Mozilla/5.0+(Windows;+U;+Windows+NT+5.1;+en-US;+rv:1.8.1.6)+Gecko/20070725+Firefox/2.0.0.6 404 0 3
Any idea how I can fix this?
Thanks!
Xavier
Ok, for the rss.xml we'll
Ok, for the rss.xml we'll need to add a rule. Under this line in your rules;
RewriteRule /xmlrpc\.php $0 [I,L]
Add the following;
RewriteRule /rss\.xml $0 [I,L]
The second part is of more concern. Which set of rules did you use? If you have Drupal installed in a sub directory you need to make sure that you are using the second set of rules, and alter the UriFormatPrefix directive so that your subdirectory name is used.
These are the rules I've
These are the rules I've used in my httpd.ini file:
[ISAPI_Rewrite]
# Accept a url with the following directories and pass them through unchanged.
RewriteRule /(?:misc|files|modules|themes|sites|uploads)/(.*) $0 [I,L]
# Make URLs sane
RewriteRule /cron\.php $0 [I,L]
RewriteRule /index\.php.* $0 [I,L]
RewriteRule /install\.php.* $0 [I,L]
RewriteRule /update\.php.* $0 [I,L]
RewriteRule /xmlrpc\.php $0 [I,L]
RewriteRule /rss\.xml $0 [I,L]
# activate rewriting for custom modules (for e.g. banner.module)
RewriteRule /banner_db\.php $0 [I,L]
RewriteRule /banner_file\.php $0 [I,L]
# deactivate rewriting for custom modules (for e.g. robotstxt.module)
RewriteRule /robots\.txt.* $0 [I,L]
RewriteRule /(.*)\?(.*) /index.php\?q=$1&$2 [I,L]
RewriteRule /(.*) /index.php\?q=$1 [I,L]
This is the structure of my webroot:
D:\mysite\root\httpd.ini (and other drupal files & folders)
IIS for my site is pointing to D:\mysite\root
settings.php is in d:\mysite\root\sites\default\settings.php
Does this look right?
Thanks!
Xavier