Simmons Consulting, the Website of Toby Simmons

WordPress Permalinks on IIS!

22
Nov

I finally got them working. The links are looking different now on my site — instead of having links that look like http://www.simmonsconsulting.com/?p=204, they are more descriptive (search engine friendly), as in http://www.simmonsconsulting.com/2005/11/22/wordpress-permalinks-on-iis/. It’s a little thing, but I have been fussing with it for several months. It’s mostly due to this post, but I had to modify the php code a bit to work on my site. I can share it if you are interested, it is a small fix.

I’ve decided to put it up because I know there are many folks out there that want permalinks on IIS without using ISAPI_Rewrite or any other addon.

First, create a file in the root of your WordPress installation. I called my wp-404-handler.php. Here are the contents of mine:

  1. < ?php
  2.   $my_wp_url = "http://" . $_SERVER['SERVER_NAME'] . "/wordpress";
  3.   $_SERVER['REQUEST_URI'] = substr($_SERVER['QUERY_STRING'], strpos($_SERVER['QUERY_STRING'], $my_wp_url)+strlen($my_wp_url));
  4.   $_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'];
  5.   include(’index.php’);
  6. ?>

You might need to edit line 2 to change the last string in quotes to be the name of your WordPress installation directory. Mine is “/wordpress” but your’s might be “/blog” or “/wp”. Once you have made that change, upload this file to the root of your WordPress directory.

Now you need create a custom 404 handler using the IIS Manager on the web server. If you have console or remote desktop access to your server, simply open the IIS Management snap-in and right-click on the web site you want to modify and select “Properties”. Now, click the “Custom Errors” tab and scroll down to the entry for 404 errors. Click the “Edit Properties” button and change the Message Type to “URL”, then type in the relative URL to your wp-404-handler.php file, in my case “/wordpress/wp-404-handler.php”.

You might need to restart IIS before the change takes effect. Happy permalinking!

Comments (37) »

  1. Constantinos says:

    Hi, can you take a look ….
    When i click on a link on my WP installation that’s in the root of my webserver i get this:
    Parse error: parse error in C:\Inetpub\vhosts\nasty.gr\httpdocs\wp-404-handler.php on line 2
    I have changed my file to look like this since i have the Wp on the root.

    what am i doing wrong?
    Thanx in advance

  2. Toby Simmons says:

    Check your file. If you copy and paste the above code, it might insert “smart quotes” which will probably mess up PHP. Try grabbing it from here.

  3. Fredrik says:

    Found this via Google, works like a treat.

    I had a lot of troubles with IISMods, URL Rewrite. It handled dynamic values really badly it seemed and due to inferior error handling it crashed my application pool after about five minutes activity.

    This solution however, worked straight out of the box, and as should be a lot easier to use than a 3rd party module for IIS since you won’t have to re-edit and/or reload anything as soon as you add a page. So, cheers!

  4. Fredrik says:

    Just noticed one problem though, seems I’m not getting proper 404 pages. It just returns a blank page. Any fix to this?

  5. Toby Simmons says:

    Did you try grabbing it from the link in comment #2?

  6. Fredrik says:

    Yes, I did. It fetches content from WordPress nicely, but as soon as it generates a real 404 Error it just returns a blank page.

  7. Fredrik says:

    Oh, a thought. Do you have “check if file exists” disabled for PHP files on your server? Was wondering if that might be what’s returning the error…

  8. Toby Simmons says:

    No, I definitely do NOT have it disabled. If you have it disabled, you cannot authenticate to a PHP page that has restricted NTFS permissions. I have “check if file exists” enabled, or ticked.

  9. msqr says:

    I have the same problem. Gives a blank page whenever I load something from it e.g. http://www.test.com/about/ Any takers?

  10. Aaron says:

    Just wanted to say thanks for such an easy and fast workaround! I tried another 404 redirect that required so much more and didn’t even work! Kudos!!!

  11. Aaron says:

    Maybe I’ve just missed it, but is there any way to specify a REAL 404 error page in your code? I’m not that great of a PHP coder, so I’m at a loss.

    Whenever I try to go to a page that doesn’t exist, I get this:

    perm404.php?404;http://mydomain.com/aardvark.asp

    (perm404.php is the name of my file with your code)

    I’d like to be able to specify an actuall 404 error page if possible. Thanks again!

  12. Julio says:

    It didn’t work for me, either…
    I get a blank page like the others… My server: Windows 2000, IIS 5.0… I dont know what to do…
    Anyone have an idea?

  13. Jann says:

    Great! Thank you so much for this. I’ve been tearing my hair out for days trying solution after solution with no success.

    This works a treat (just got to transfer 5 years of archives from my home-grown blog system now!)

  14. herohacker says:

    I want use your system, but i have my index.php out of his directory

    http://www.mywebsite.com/index.php (this is the index of core wordpress)

    and my core files are in http://www.mywebsite.com/blogweb/

    how can i configure the script for this works on my server?
    I try use normally and ever give me errors in the images.

  15. Sunil Parmar says:

    Hi!!!
    I used your concept and when i set my Permalink structure to /%postname%/.
    It gives ‘NO MATCHING ENTRIES FOUND’ for every page i’ve made and also for the posts.
    Is there any solution?
    I couldn’t get what u meant by restart your IIS. The only control for IIS i’ve is Custom errors…i don’t know how to restart my IIS.
    Thank you.

  16. Toby Simmons says:

    Restart IIS means to restart the IISAdmin service. You can do this by bringing up a command window on the server and entering the command

    iisreset /restart

    This will stop and restart the server’s web services. Does that make sense?

  17. Gordon says:

    I wonder if you could help me… Im using WP on IIS… and love the idea of your script… It just *would not* work for me… so knowing no real PHP, I managed to hack together this…


    To make it work :-D

    Now… the problem is, that ANY content which checks the users role / level etc now just ignores them… so my logged in users with special content for them, see everything as if they are unregistered (particularly sidebar stuff).

    Thanks loads,

    Gordon

  18. Gordon says:

    Ooops, my code didnt appear:

    I thought i was being SO clever!! :D

  19. Gordon says:

    Urr… this really doesnt want to let me use code tags… its late, and im tired, sorry for the excess commenting – Im just typing the code below now without code tags…

    $my_wp_url = “http://” . $_SERVER[‘SERVER_NAME’] . “:80/” ;
    $mybusyurl = “http://www.thebusyproject.org.uk/index.php/” . substr($_SERVER[‘QUERY_STRING’], strpos($_SERVER[‘QUERY_STRING’], $my_wp_url)+strlen($my_wp_url));
    $_SERVER[‘REQUEST_URI’] = substr($_SERVER[‘QUERY_STRING’], strpos($_SERVER[‘QUERY_STRING’], $my_wp_url)+strlen($my_wp_url));
    $_SERVER[‘PATH_INFO’] = $_SERVER[‘REQUEST_URI’];
    $checkurl = substr($_SERVER[‘QUERY_STRING’], strpos($_SERVER[‘QUERY_STRING’], $my_wp_url)+strlen($my_wp_url));
    switch ($checkurl) {
    case computerclub:
    include(‘http://www.thebusyproject.org.uk/index.php/activities/computer-club’);
    break;
    default:
    include($mybusyurl);
    }

  20. djwhisky says:

    I’ve tried to set this up on my WordPress blog that’s sitting in the root of my domain… but whenever i try to go to a permalink page its redirecting to the WordPress admin site… any ideas why?

  21. djwhisky says:

    Have fixed it, but i had to put the full path to the include…
    i.e. include(‘d:/websites/……/index.php’);
    any ideas why?

  22. 808Talk says:

    I noticed on this post (Comment 4 and 6) and also at the WordPress site http://wordpress.org/support/topic/36214 that others are getting a blank page when an actual 404 is reached instead of a redirection to the WordPress 404 template. Has anyone figured out why? I did a check at http://www.rexswain.com/httpview.html and noticed that the header received from my error page is an actual 404 code, so why is this not redirecting to the WordPress 404 template? Please help!

    This works like it’s supposed to here:
    http://www.808talk.com/2007/02/02/808ttv-e51-waikiki-kite-surfing/

    This one is an actual 404 and shows a blank:
    http://www.808talk.com/2007/02/02/808ttv-e51-waikiki/

    Aloha

  23. luke says:

    I am trying to set up WPMU on IIS (W2K) and want to use your “hack”. I finally got it working by including the full physical path as djwhisky said, but now the multiple blogs of MU are causing problems – you can see that you get a WP 404 when trying to access a new “sublog” that i created here: http://lmueller.dsl.visi.com/thedoor/wpmu2/bloggomatic/

    but the 404 fix is working for normal posts: http://lmueller.dsl.visi.com/thedoor/wpmu2/blog/2007/02/22/hello-world/

    although i’m confused as to why the URL is: http://lmueller.dsl.visi.com/thedoor/wpmu2/wp-signup.php?new=%2Fhello-world%2F when you get there…

    any ideas on how i can get it to work with MU ?

    Thanks!

  24. Eddie Walter says:

    Hi Toby,

    I’m having a problem that someone else was having, but I don’t see a response. I hope you can help me because I really want to get the index.php out of my permalinks. Your workaround works great except when I take the index.php out of the structure. When I take it out, I get “Sorry, no posts matched your criteria.” on the homepage and a blank page for any post or other page I try to go to.

    Your help would be greatly appreciated!

  25. […] WordPress Permalinks on IIS, do Simons Consulting, utilizando apenas 404 redirecting […]

  26. I also get the blank page instead of my theme’s 404.php file when a user hits a real 404. That’s interesting that you have “Check if file exists” ticked in IIS. I am baffled as to why this is happening. On my localhost copy of WordPress, it works fine. On my host’s servers it doesn’t work though and the page comes up blank.

  27. Toby Simmons says:

    For all the folks getting blank pages on 404s, what version of WP are you using? I don’t seem to be having a problem at all with the latest 2.2.2 (and haven’t had a problem since 2.0)

  28. Klaus T. says:

    The example in this post didn’t work for me, out of the box. There was a [ instead of a ( and then I had to add :80 to the URL for it to work (and remove the /wordpress from it).

    Here’s what worked for me in Win2003 Web Edition with Plesk 8.1 (and php 4.something):

    $my_wp_url = “http://” . $_SERVER[‘SERVER_NAME’] . “:80” ;
    $_SERVER[‘REQUEST_URI’] = substr($_SERVER[‘QUERY_STRING’], strpos($_SERVER[‘QUERY_STRING’], $my_wp_url)+strlen($my_wp_url));
    $_SERVER[‘PATH_INFO’] = $_SERVER[‘REQUEST_URI’];
    include(‘index.php’);

  29. whirlpool says:

    worked perfectly. beautiful work. thanks a lot.

  30. Andrew says:

    Thank God for this work around! I’ve been battling this for weeks!!!!

  31. […] to Simmons Consulting for helping me out with the perma links issue. Hello there! If you’re new here, you may want to […]

  32. […] Simmons Consulting » WordPress Permalinks on IIS!:(英語) […]

  33. Joe says:

    Thank you for the code, even though it’s 6 years old it still works and works with WP 3.1!

    The only question I have is the only way I can get this to work though is to keep “/index.php/” at the front of my permalinks line. I’ve tried to remove it and I end up just getting the code for the php error handler file in the browser. If I leave it in there everything works fine.

    Any ideas?

  34. StevenBullen says:

    I couldn’t get this to work and after some hunting around I found the following code that worked for me.

    Hope it helps someone out.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>