Enabling Pretty Permalinks in WordPress

Author: Ruslan Yakushev

Published on May 30, 2008 by ruslany

Updated on November 05, 2009 by ruslany

Average Rating  Rate It (1)

RSS

This walkthrough will guide you through how to enable “Pretty Permalinks” for blog posts in the WordPress blog engine installed on IIS 7.0. Typically, without URL rewriting functionality on a Web server, WordPress users have to use “Almost Pretty” URLs (for example, http://contoso.com/index.php/yyyy/mm/dd/post-name/). This was the primary option for users who chose to host WordPress on IIS. Now, with the URL rewrite module available, you can have “Pretty Permalinks” (for example, http://example.com/year/month/day/post-name/) for WordPress blogs hosted on IIS 7.0.

Prerequisites

This walkthrough requires the following prerequisites:

  1. IIS 7.0 with FastCGI and PHP installed. (If you need to install PHP, follow the instructions in this article.)
  2. WordPress installed. (Follow the instructions in this article or use the instructions from the official WordPress site.)
  3. URL rewrite Go Live release installed.

Note that for the purposes of this walkthrough it is assumed that WordPress is installed in a Web site root directory. If WordPress is installed in a subdirectory, then the rewrite rules that are used in this walkthrough should be included in the Web.config file located within the same subdirectory where the WordPress files are.

Enabling pretty permalinks in WordPress

Once WordPress is installed, log on to it as an administrator, and then click the “Options” tab. Then click the “Permalinks” subtab. This will take you to the page where you can customize how WordPress generates permalinks for blog posts.

On this page, choose the “Custom, specify below” option and enter
“/%year%/%monthnum%/%day%/%postname%/” in the “Custom structure” text box.

Click Update Permalink Structure.

Now, you will see that all the blog post links have URLs that follow the format you have specified, but if you click any one of those links the Web server will return a 404 - File Not Found error. This is because Wordpress relies on a URL rewriting capability within a server in order to rewrite requests that have “pretty permalinks” to an Index.php file. In the next section we will create a rule that will provide this capability.

Creating rewrite rule

Create and open Web.config file located in the same directory where WordPress files are installed, and paste the following XML section into the system.webServer element.

<rewrite>
    <rules>
        <rule name="Main Rule" stopProcessing="true">
            <match url=".*" />
            <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            </conditions>
            <action type="Rewrite" url="index.php" />
        </rule>
    </rules>
</rewrite>

This rule matches any requested URL and if that URL does not corresponds to a file or a folder on a file system, then it will rewrite the URL to Index.php. At that point, WordPress will determine which content to serve based on the REQUEST_URI server variable that contains the original URL before it was modified by the rule.

Testing the rewrite rule

After the rewrite rule is saved to the Web.config file, open a Web browser and click any one of the permalinks in WordPress blog–you should see the correct content returned by the Web server for every permalink:

Summary

In this walkthrough you have learned how to use the URL rewrite module to enable “pretty permalinks” in the WordPress blog engine. WordPress is just one example of the many popular PHP applications that can take advantage of the URL rewrite module in IIS 7.0, a feature in a system that enables user friendly and search engine friendly URLs.

Related Content

Comments

  1. Submitted on Jun 10 2008 by
    danielvl
    As a side note, when you have a condition match type equals to IsFile or IsDirectory, the default input value is always "{REQUEST_FILENAME}", so you could have a rule like:

    rewrite>
    rules>
    rule name="Main Rule" stopProcessing="true">
    match url="." />
    conditions>
    add matchType="IsFile" negate="true" />
    add matchType="IsDirectory" negate="true" />
    /conditions>
    action type="Rewrite" url="index.php" />
    /rule>
    /rules>
    /rewrite>
  2. Submitted on Mar 20 2009 by
    ahmetkemal
    Hello,

    Firstly, thanks for this great tutorial. I configured my Wordpress blog and working very good. There is only one problem. My page couldn't be seen when I tried directly access to URL. I mean when I try to enter www.mydomain.com with browser 404 error returns. I think browser could't find index.php . After entering www.mydomain.com/index.php all other pages work with no problem.

    How can I configure web.config to be able to fix that problem?

    Thank you.
  3. Submitted on Mar 27 2009 by
    ruslany
    Add the following XML element inside of the system.webServer> node of your web.config file:

    defaultDocument>
    files>
    remove value="index.php" />
    add value="index.php" />
    /files>
    /defaultDocument>
  4. Submitted on Apr 16 2009 by
    amccarter
    I have this rule set up, but no matter which permalink I go to, it just shows my index page. Did I set something up incorrectly?
  5. Submitted on Apr 16 2009 by
    amccarter
    Oh, also, I'm running PHP via ISAPI... could this be the problem?
  6. Submitted on Jun 02 2009 by
    ruslany
    Yes. Permalinks will not work when PHP is run via ISAPI.
  7. Submitted on Jun 06 2009 by
    ruslany
    TO ALL: PLEASE POST YOUR QUESTIONS ABOUT THE MODULE HERE: http://forums.iis.net/1152.aspx. YOU WILL GET MUCH FASTER RESPONSE!
  8. Submitted on Sep 06 2009 by
    GoodThings2Life
    For those who may be running WordPress in an environment that needs to coexist with Exchange 2007's Outlook Web Access, you will want to add the following condition to your rule:

    {PATH_INFO} Does not match the pattern /owa/

    This will prevent the URL Rewrite rules from applying to the OWA pages (which results in Access Denied errors).
  9. Submitted on Sep 11 2009 by
    sbryan
    I haven't found an official set of instructions for IIS 6, yet. I have found some plug-ins and unattractive band-aids, which I'm reluctant to implement.

You must Log In to comment.