Adsense

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:

Anonymous said...

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

Rebecca said...

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!

Paul Fox said...

Happy to have helped!

Andrew D. said...

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

Also, I saw this online





Is this bascially the same thing?

Andrew D. said...

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

Anonymous said...

Thanks. That came in very handy.

Anonymous said...

Man, you rocks!

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

Thanks!

Russ S. said...

Awesome! Thanks for the help. Fixed problem.

Unknown said...

Thanks, that solved the problem :)
Regards form Colombia

Unknown said...

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

juan said...

<3 U!!

Anonymous said...

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

Unknown said...

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.

Outsider said...

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