Saturday, October 15, 2011

IIS7 error when enabling Wordpress Permalinks URL Rewriting

I wanted to turn on Permalinks in Wordpress to use the following custom structure.

/%year%/%postname%/


I added the following section to my web.config file.

<rewrite>
    <rules>
 <rule name="wordpress" patternSyntax="Wildcard">
     <match url="*" />
     <conditions>
  <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>

After updating the web.config and browsing to one of my posts, I encounter the following IIS7 error:
Cannot add duplicate collection entry of type 'rule' with unique key attribute 'name' set to 'wordpress'


When I’ve encountered this error in the past, it hasn’t been a direct problem with my web.config, but has been a problem with inherited sections or values from a parent website config or one of the default environment configurations. With that in mind, I added the following line to remove any existing rules with the name “wordpress” before trying to add my own.

<remove name="wordpress"/>
The complete web.config section looked like this and cleared up the error.
<rewrite>
    <rules>
 <remove name="wordpress"/>
 <rule name="wordpress" patternSyntax="Wildcard">
     <match url="*" />
     <conditions>
  <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>

Hope this helps someone!

14 comments:

  1. Anonymous10:38 PM

    How are you sure the wordpress rule that you removed wasn't needed somewhere else though?

    ReplyDelete
  2. Rebecca8:06 AM

    Thank you!!! You are a genius. I was struggling with this same issue and this resolved it beautifully-- and now I think I understand WHY it was an issue, a little better!

    ReplyDelete
  3. Happy to have helped!

    ReplyDelete
  4. Are you changing this on the parent web.config file or the child?

    Also, I saw this online





    Is this bascially the same thing?

    ReplyDelete
  5. Well that didn't display code so here is where I found a similar solution: http://www.geefamily.net/2010/08/22/wordpress-and-subdomains

    ReplyDelete
  6. Anonymous2:26 PM

    Thanks. That came in very handy.

    ReplyDelete
  7. Anonymous4:45 PM

    Man, you rocks!

    Finally I could run my site http://hectorea.com/blog

    Thanks!

    ReplyDelete
  8. Russ S.5:49 PM

    Awesome! Thanks for the help. Fixed problem.

    ReplyDelete
  9. Thanks, that solved the problem :)
    Regards form Colombia

    ReplyDelete
  10. thank you Genius, solved my problem like a boss. <3 from Pakistan

    ReplyDelete
  11. Anonymous2:36 PM

    Thank you! Great solution, everything appears to be working.

    ReplyDelete
  12. Worked for me, after an hour of trying to figure out how something as simple as changing a permalink could brake the whole site. Thanks.

    ReplyDelete
  13. This was great advice, it pointed me in the right direction however my issue was I had the same rule duplicated, just had to delete the one

    ReplyDelete