WordPress Permalink and IIS

When wordpress is hosted in IIS, permalink will not work, as permalink needs url rewrite.

WordPress is a php application aimed to be hosted by Apache server, there is a rewrite_module of Apache which is doing url rewrite through .htaccess file.

All these mechanisms do not exist in out of box IIS.

In IIS 7 you can use built in url rewrite module, as the format of redirect rules are quite different from Apache ones, so I choose to use ISAPI rewrite from Helicon Tech, the lite version is free and it works for IIS6 too. The main reason I use this is that the rewrite rules are the same as ones in .htaccess. So you do not need to learn things twice.

1. Download from here, choose lite version marked as freeware

2. Install it on IIS server

3. Go to IIS php site(how to set up php in IIS), right click, properties, ISAPI Filters, Add, give it a name and point executable to :

C:\Program Files\Helicon\ISAPI_Rewrite3\ISAPI_Rewrite.dll

4. Then go to C:\Program Files\Helicon\ISAPI_Rewrite3\ , edit httpd.conf, httpd.conf is redirect rule file for whole server, you cannot have .htaccess files for different folders as this is one of the limitations of lite version, but with bit of work, you can config httpd.conf to work with multiple virtual directories

5. Here is the recommended rewrite rules for wordpress in terms permalink

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

I will explain a little bit of these rules to you:

First line: if index.php , ‘-‘ means no redirect, and [L] means read no more. If it does not match, jump to line 4, as it is only looking for ‘RewriteRule’ directive.

Line 4: redirect every url to index.php, suppose all the rewrite conditions are met, as for ‘RewriteCond’ engine is back referencing , so if matched, jump to line 2

Line 2 : if it is a file is requested , no redirect
Line 3 : if it is a folder is requested, no redirect, adding them up means if request is not a file, nor a folder, then redirect to index.php, and let index.php to process until then permalinks will be understood and served with right content.

6. To turn on the logging for isapi_rewrite, put these two lines at the top in httpd.conf

RewriteLogLevel 9
LogLevel debug

You should be able to see rewrite at work in file rewrite.log under same folder

7. For those who have multiple wordpress installs on same iis, here is example httpd.conf

RewriteLogLevel 9
LogLevel debug

RewriteBase /

RewriteRule ^wordpress1/index\.php$ - [L]

RewriteRule ^ wordpress2/index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^ wordpress1/.*$ / wordpress1/index.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^ wordpress2/.*$ / wordpress2/index.php [L]

A few points:
7.1. Logging is turned on
7.2. Two sample wordpress sites are:
http://yoursite.com/wordpress1,
http://yoursite.com/wordpress2

8. URL and permalink are always CASE SENSITIVE , even in IIS.

To IIS, urls are not case sensitive, but to wordpress they are, even you make rewrite rules case insensitive , still when it comes to wordpress, if cases do not match, then a 404 page will be served.

Tags: ,

This entry was posted on Thursday, December 23rd, 2010 at 2:55 am and is filed under PHP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

*