Posted By

thesmu on 03/06/13


Tagged

wordpress


Versions (?)

WordPress, IIS, Permalinks and index.php | Richard Shepherd


 / Published in: PHP
 

URL: http://richardshepherd.com/wordpress-iis-permalinks-and-index-php/

WordPress, IIS, Permalinks and index.php | Richard Shepherd

  1. Here’s the problem:
  2.  
  3. You need to install WordPress on a Windows machine running IIS. I’d never normally do this, but sometimes you just don’t have a choice.
  4.  
  5. There are plenty of links out there about how to do this, and the one I referred to most was How To Install WordPress on IIS 6.0. It is, more or less, straightforward.
  6.  
  7. However, there’s an issue with IIS and permalinks.The issue is that if you want to take advantage of the WordPress pretty permalinks, you have to suffer a folder in the path called ‘index.php’. So, instead of the rather lovely:
  8.  
  9. http://www.yourblog.com/yourcategories/the-best-post-in-the-world/
  10.  
  11. you have to grit your teeth and accept:
  12.  
  13. http://www.yourblog.com/index.php/yourcategories/the-best-post-in-the-world/
  14.  
  15. I don’t know why, I really don’t. I’m not that smart. But I did work out how to fix it. It’s easy enough, but you have to put a couple of rules in the ISAPI ReWrite Engine to deal with the quirks.
  16.  
  17. First we need to head into our WordPress admin tool, and go to Settings > Permalinks. There, you’ll see the craft insertion of /index.php/ in the links. We need to create a custom permalink and delete the index.php bit. Just like this…
  18.  
  19.  
  20.  
  21. Now save that off and head over to your .htaccess file – we need to weave some magic!
  22.  
  23. At the bottom of this post is the whole file, but the two lines I particularly want to point out are:
  24.  
  25. 1
  26. 2
  27. RewriteCond %{REQUEST_URI} !/wp-admin
  28. RewriteRule ^/(.*)/$ /index.php/$1 [NC]
  29. The second line works the juju. It takes the URL from the browser, shoves an /index.php/ into it, and displays the contents. For more on how and why this works, check out http://www.workingwith.me.uk/articles/scripting/mod_rewrite for the basics.
  30.  
  31. Now the first line is really important, because it tells the server not to do this if we’re in the admin section. If we remove this line, there are all kinds of problems reaching /wp-admin/index.php. Trust me.
  32.  
  33. I have two other lines in the following code which get rid of index.php from URLS, which stops the google duplicate content issue. They’re always worth having.
  34.  
  35. Finally, make sure that wherever your site is hosted, that the correct permissions are set on the wp-admin and wp-content directories and their sub-directories. You must have read/write/modify permissions set for internal user accounts.
  36.  
  37. If you implement these small tweaks, then your WordPress IIS install should work like a charm.
  38.  
  39. Good luck!
  40.  
  41. Here’s the code:
  42.  
  43. 1
  44. 2
  45. 3
  46. 4
  47. 5
  48. 6
  49. 7
  50. 8
  51. 9
  52. 10
  53. 11
  54. 12
  55. 13
  56. 14
  57. 15
  58. 16
  59. 17
  60. 18
  61. 19
  62. 20
  63. 21
  64. 22
  65. 23
  66. 24
  67. 25
  68. 26
  69. 27
  70. 28
  71. 29
  72. 30
  73. 31
  74. 32
  75. 33
  76. 34
  77. 35
  78. 36
  79. 37
  80. 38
  81. 39
  82. 40
  83. 41
  84. 42
  85. 43
  86. 44
  87. 45
  88. 46
  89. 47
  90. 48
  91. 49
  92. 50
  93. 51
  94. # -------------------------------------------------------------------------
  95. # Kickstart the Rewrite Engine and set initial options
  96. # -------------------------------------------------------------------------
  97.  
  98. RewriteEngine On
  99. RewriteCompatibility2 On
  100. RepeatLimit 200
  101. RewriteBase
  102.  
  103. # -------------------------------------------------------------------------
  104. # WORDPRESS, WINDOWS & ISAPI DOCUMENTATION
  105. # -------------------------------------------------------------------------
  106. # These are the WordPress redirects we need in place for a Windows Server
  107. # running PHP & MySQL
  108. # We had some issues configuring ISAPI with WordPress and so here are the
  109. # solutions in case we need them again!!
  110.  
  111. # -------------------------------------------------------------------------
  112. # PERMISSIONS SETTINGS
  113. # -------------------------------------------------------------------------
  114. # The following folders:
  115. # wp-admin
  116. # wp-content
  117. # MUST HAVE read/write/modify permissions set for internal user accounts
  118.  
  119. # -------------------------------------------------------------------------
  120. # ESSENTIAL WORDPRESS REWRITES
  121. # -------------------------------------------------------------------------
  122.  
  123. # Redirects 'www.yourblogsite.com/index.php' to 'www.yourblogsite.com/'
  124.  
  125. RewriteRule ^/index.php$ / [NC,P,R=301]
  126.  
  127. # Rewrite 'www.yourblogsite.com/anything/' to 'www.yourblogsite.com/index.php/anything/'
  128. # NB. The user does not see this rewrite.
  129. # Must also go into WordPress > Settings > Permalinks and select 'custom'
  130. # and then REMOVE 'index.php' from the custom URL it generates
  131.  
  132. # The first line is a condition so it doesn't apply the rule to the wp-admin part of the site
  133.  
  134. RewriteCond %{REQUEST_URI} !/wp-admin
  135. RewriteRule ^/(.*)/$ /index.php/$1 [NC]
  136.  
  137. # Finally, redirect 'www.yourblogsite.com/anything/anything/index.php'
  138. # to 'www.yourblogsite.com/anything/anything/'
  139.  
  140. RewriteRule ^/(.*)/index.php$ /$1/ [NC,P,R=301]
  141.  
  142. # -------------------------------------------------------------------------
  143. # END OF ESSENTIAL REWRITES
  144. # -----------------------------

Report this snippet  

You need to login to post a comment.