Return to Snippet

Revision: 62700
at March 6, 2013 22:20 by thesmu


Initial Code
Here’s the problem:

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.

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.

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:

http://www.yourblog.com/yourcategories/the-best-post-in-the-world/

you have to grit your teeth and accept:

http://www.yourblog.com/index.php/yourcategories/the-best-post-in-the-world/

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.

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…



Now save that off and head over to your .htaccess file – we need to weave some magic!

At the bottom of this post is the whole file, but the two lines I particularly want to point out are:

1
2
RewriteCond %{REQUEST_URI} !/wp-admin
RewriteRule ^/(.*)/$ /index.php/$1 [NC]
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.

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.

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.

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.

If you implement these small tweaks, then your WordPress IIS install should work like a charm.

Good luck!

Here’s the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -------------------------------------------------------------------------
# Kickstart the Rewrite Engine and set initial options
# -------------------------------------------------------------------------
 
RewriteEngine On
RewriteCompatibility2 On
RepeatLimit 200
RewriteBase
 
# -------------------------------------------------------------------------
# WORDPRESS, WINDOWS & ISAPI DOCUMENTATION
# -------------------------------------------------------------------------
# These are the WordPress redirects we need in place for a Windows Server
# running PHP & MySQL
# We had some issues configuring ISAPI with WordPress and so here are the
# solutions in case we need them again!!
 
# -------------------------------------------------------------------------
# PERMISSIONS SETTINGS
# -------------------------------------------------------------------------
# The following folders:
# wp-admin
# wp-content
# MUST HAVE read/write/modify permissions set for internal user accounts
 
# -------------------------------------------------------------------------
# ESSENTIAL WORDPRESS REWRITES
# -------------------------------------------------------------------------
 
# Redirects 'www.yourblogsite.com/index.php' to 'www.yourblogsite.com/'
 
RewriteRule ^/index.php$ / [NC,P,R=301]
 
# Rewrite 'www.yourblogsite.com/anything/' to 'www.yourblogsite.com/index.php/anything/'
# NB. The user does not see this rewrite.
# Must also go into WordPress > Settings > Permalinks and select 'custom'
# and then REMOVE 'index.php' from the custom URL it generates
 
# The first line is a condition so it doesn't apply the rule to the wp-admin part of the site
 
RewriteCond %{REQUEST_URI} !/wp-admin
RewriteRule ^/(.*)/$ /index.php/$1 [NC]
 
# Finally, redirect 'www.yourblogsite.com/anything/anything/index.php'
# to 'www.yourblogsite.com/anything/anything/'
 
RewriteRule ^/(.*)/index.php$ /$1/ [NC,P,R=301]
 
# -------------------------------------------------------------------------
# END OF ESSENTIAL REWRITES
# -----------------------------

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

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

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

Initial Tags
wordpress

Initial Language
PHP