Micronovae
Home Products Downloads Purchase Support Company
Overview Documentation Download Purchase FAQ
 
IIS Mod-Rewrite

Clean Permalinks for Wordpress on IIS using .htaccess

27 Jan 2008

This article describes a detailed, step-by-step guide for enabling Clean Permalinks for Wordpress on IIS web server

This solution requires IIS Mod-Rewrite Pro and IIS 6.0 or later, and it is tested with Wordpress 2.x or Wordpress MU 1.3, PHP 5.2.4, and MySQL 5.0.24.

To enable Clean Permalinks on Wordpress on IIS, follow these steps:

  IIS Mod-Rewrite

Download IIS Mod-Rewrite

Purchase IIS Mod-Rewrite
  1. Install PHP following the vendor's instructions.

  2. Install MySQL Server following the vendor's instructions.

  3. Install IIS Mod-Rewrite Pro (download).

  4. If on production environment, activate IIS Mod-Rewrite using a valid license key. If activation is skipped, IIS Mod-Rewrite will work in test mode and you will need to restart IIS every 30 minutes or 1000 requests, if your testing exceeds these limits.

  5. Create an empty MySQL database for wordpress.

  6. Install Wordpress according to the vendor's instructions.

  7. Open with a text editor the file wp-settings.php, located in Wordpress root directory.

  8. Add the code line

    $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];

    at the beginning of wp-settings.php script. After adding this line of code, the beginning of wp-settings.php script should look like this:

    <?php
    $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];
    ...
    ...

  9. Log on to Wordpress as administrator, go to admin panel, open the "Permalinks" settings page, and set the permalink structure of your choice. Apply your changes.

  10. If the permalink structure is automatically prefixed with "/index.php", you can safely manually remove it in order to have absolutely clean permalinks. Apply your changes.

  11. Wordpress is supposed to automatically create a file with name ".htaccess" in its root directory. However, if no ".htaccess" file is created, you can create one using a text editor and copying the following URL rewriting configuration in it:

    RewriteEngine On

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

    If using Wordpress-MU (multi-users), copy this configuration instead:

    RewriteEngine On

    #RewriteBase /

    #uploaded files
    RewriteRule ^(.*/)?files/$ index.php [L]
    RewriteRule ^(.*/)?files/(.*) wp-content/blogs.php?file=$2 [L]

    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule . - [L]
    RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-.*) $2 [L]
    RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
    RewriteRule . index.php [L]

After, completing all the steps above, your Wordpress installation will fully serve clean, search engine friendly URLs, and your website's SEO will be drastically improved.


For advanced users

In order to avoid the last two steps, advanced users that are familiar with PHP coding can also perform the following tasks in step 8.

  • Open the file "misc.php" located at [Wordpress root]\wp-admin\includes\misc.php
  • Locate the function got_mod_rewrite() at the beginning of the script.
  • If using Wordpress 2.5, add to the existing script the code line shown below in red:

    function got_mod_rewrite() {
       $got_rewrite = apache_mod_loaded('mod_rewrite', true);
       /*Add the line below*/
       if( isset( $_SERVER['HTTP_X_ORIGINAL_URL'] ) )$got_rewrite = true;

       return apply_filters('got_rewrite', $got_rewrite);
    }

  • If using Wordpress 2.3 or Wordpress MU 1.3, add to the existing script the code line shown below in red:

    function got_mod_rewrite() {
       /*Add the line below*/
       if( isset( $_SERVER['HTTP_X_ORIGINAL_URL'] ) )return true;

       global $is_apache;

       // take 3 educated guesses as to whether or not mod_rewrite is available
       if ( !$is_apache )
          return false;

       if ( function_exists( 'apache_get_modules' ) ) {
          if ( !in_array( 'mod_rewrite', apache_get_modules() ) )
          return false;
       }

       return true;
    }

  • If using Wordpress 2.x earlier than 2.3, you must do the same as with Wordpress 2.3, but you should locate the function got_mod_rewrite() in the file [Wordpress root]\wp-admin\admin-functions.php
  • If using Wordpress MU 1.3.x, you must do the same as with Wordpress 2.3, but you should locate the function got_mod_rewrite() in the file [Wordpress root]\wp-admin\admin-functions.php

By adding this line of code, you avoid the automatic prefix of /index.php in the the permalink structure in step 10, and also the appropriate .htaccess is created automatically avoiding step 11. Of course, IIS must have sufficient access rights for Wordpress to be able to create this file in the root directory of Wordpress installation.