IIS Mod-Rewrite solutions
Overview Documentation Download Purchase FAQ  
IIS Mod-Rewrite - URL rewrite tool for IIS

Clean Permalinks for Wordpress on IIS using .htaccess

26 Jun 2010

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 and later or Wordpress MU 1.3.x and later, PHP 5.2.4, and MySQL 5.0.24.

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

  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 remove it manually 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 (including the commented lines):

    # BEGIN WordPress
    RewriteEngine On

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

    # END WordPress

    If using Wordpress-MU (multi-users), rename the file htaccess.dist located in Wordress-MU root directory to .htaccess, or create a new .htaccess file and copy the following configuration in it:

    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]


  12. Make sure IIS has sufficient access rights to the file .htaccess. The user group IIS_WPG (if running on IIS 6), or the user group IIS_IUSRS (if running on IIS 7), or the current Application Pool's Identity User must have at least "read" access rights to the ".htaccess" file or to the directory that contains this file.

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 3 steps (10 through 12), advanced users that are familiar with PHP coding can also perform the following tasks in step 8.

  • Make sure IIS has full access rights to Wordpress root directory. The user group IIS_WPG (if running on IIS 6), or the user group IIS_IUSRS (if running on IIS 7), or the current Application Pool's Identity User must have "full" access rights to Wordpress root directory.
  • 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 or later or Wordpress MU 1.5 or later, 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.

 
 
Download and try
IIS Mod-Rewrite
NOW!


IIS rewrite

Download IIS Mod-Rewrite

Purchase IIS Mod-Rewrite