IIS Rewrite WordPress User Authenticated

I want to be able to rewrite a url using url rewriting in IIS 7 whenever an anonymous user hits the home page of my wordpress site. Is there a way to identify whether the user is logged in or not as a condition of a rewrite rule?

Related posts

Leave a Reply

1 comment

  1. Turns out this can be done by inspecting the cookie. In my case, I check to see whether HTTP_COOKIE contains the text ‘wordpress’. If it does not, then I rewrite to a cached version of my page.

    I also include a check to make sure we are on the www subdomain to avoid any conflict with another of my rewrite rules that is run when the url is on mydomain.com.

    <rule name="HomePageCacheRewriteRule" patternSyntax="ECMAScript" stopProcessing="true">
        <match url="^$" />
        <conditions logicalGrouping="MatchAll">
            <add input="{HTTP_COOKIE}" negate="true" pattern="wordpress" />
            <add input="{HTTP_HOST}" pattern="www.mydomain.com" />
        </conditions>
        <action type="Rewrite" url="cached-home-page.htm" />
    </rule>
    

    The result is that anonymous users get the cached page, while logged in / recently logged out users see the standard page loaded by WordPress.

    Note: There are some additional checks that should possibly be added to handle some other scenarios. Here is a good article with a more robust example http://ruslany.net/2008/12/speed-up-wordpress-on-iis-70/