Keyboard Face

when you find QWERTY imprinted on your cheek - it's time to go to bed.

Tags

asp CSS css menus dreamweaver flash gallery2 hosting iis7 Javascript jquery jquery ui left mac mssql os9 php printer rdp SEO sql sql server windows wordpress

Update for WordPress Permalinks on IIS

September 7, 2007 by Tom 56 Comments

There has been quite a bit of comments going back and forth on the code that I wrote to enable permalinks for WordPress on IIS since I put it up on the site. Just recently, Einar Egilsson posted an extremely helpful bit a code in the comments. It’s a much better and cleaner way of accomplishing the same thing.

I was always aware that my solution was a little odd in that it used ASP. WordPress is written in PHP and to use ASP requires odd work-arounds. Einar gave us a great simple solution that uses PHP. It too requires 404 redirects, though.

It’s requirements are:

  • IIS for Windows
  • WordPress
  • Ability to change your 404 error page with your web host.

To install, copy and paste this text into a file named wp-404-handler.php:

< ?php
$qs = $_SERVER['QUERY_STRING'];
$_SERVER['REQUEST_URI'] = substr($qs, strpos($qs, ':80')+3);
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'];
include('index.php');
?> 

Next set the 404 error page to /wp-404-handler.php. If it gives you a choice between file and url, choose url.

Lastly, make sure you enable permalinks in the WordPress control panel under Options > Permalinks.

That should do it.

Thanks to Einar for posting this in the comments and thanks to CaptainN who first wrote about in the WordPress support forums.

Filed Under: Installing Wordpress

Comments

  1. Sal B says

    October 26, 2007 at 12:40 pm

    THANK YOU SO MUCH! Dude, I have been racking my brain with this ever since I installed WP. I was [—-] this close to switching my hosting plan (which I will one day anyways) to a linux box – but now i have this solution!! THANK YOU!

  2. Sal B says

    October 26, 2007 at 12:49 pm

    P.S.

    Is this a bot killer?? IE – will I lose traffic due to this? Please email me about this.

  3. Tom says

    October 26, 2007 at 9:43 pm

    I’m glad it helped.

    No it’s not a bot killer. It is the exact opposite. It makes your site SEO friendly by removing the ?’s.

  4. Sal B says

    October 27, 2007 at 9:09 am

    Thanks again!

  5. Luke says

    November 1, 2007 at 8:56 am

    Hi,

    thanks, it works great. I just had to delete the free space at the end, between ? and > .

    But now a question. Why the URL is changing into one without www ??? Can I change it somehow?

  6. Will says

    November 15, 2007 at 10:29 pm

    Hi, do you know if that will work with wordpress mu?

  7. rob says

    December 5, 2007 at 5:32 pm

    Hi, I’m using a custom 404.php page on an IIS server.
    I’m using the wp-404-handler.php and set-up the server as you described but when I enter a bad url I get an error:

    Fatal error: Call to undefined function get_header() in C:\Inetpub\wwwroot\blog\wp-content\themes\glossyblue-1-4\index.php on line 1

    Any idea what might be causing the error?

  8. 5ivedance says

    December 18, 2007 at 8:30 pm

    My Permalinks Include The “index.php”
    But I dislike T-T

  9. Hep says

    January 8, 2008 at 7:17 am

    Nice post!
    However, I have a small problem. IIS doesn’t seem to pass $_POST and $_GET variables this way, at least my print_r ($_POST); returns nothing if I put it in 404-handler.php.
    It’s a big problem, since I have on my blog…

  10. Thomas Sinfield says

    January 14, 2008 at 3:08 am

    Thankyou so much! I have been looking for something like this for ages and you are the first place that has a working answer!

  11. Lazlow says

    February 17, 2008 at 11:07 am

    Thanks! I couldn’t get it working at first, but then realised I also needed /wp in the URL as I’ve got WordPress installed in a sub-directory.

  12. Mike Boudet says

    April 2, 2008 at 10:26 am

    Thank you so much for this! It works awesome!

  13. Diana says

    April 12, 2008 at 5:30 pm

    Thanks! This worked great.

    One small issue. Now when I type in a wrong url, I just get a blank white page with no code at all, rather than a WordPress page not found error.

    It DOES appear to be making it to the index.php page, but then it blows up somewhere I guess. I didn’t have a 404.php page in my theme and thought that might be causing it, but I copied the one from the default theme and it didn’t help.

    Has anyone else seen this problem?

  14. Mike says

    May 30, 2008 at 8:22 am

    THANKS! WORKS PERFECTLY!

  15. Clint says

    June 9, 2008 at 3:03 pm

    There is a typo in this code snippet. The last line should read “?>” instead of “? >”. With the typo, you will just get a blank page like Diana complained about.

  16. Tom says

    June 9, 2008 at 11:14 pm

    Thanks! That’s a good catch. I updated it.

  17. Clint says

    June 10, 2008 at 7:41 am

    NP. Thanks for posting this, this solution worked great for my installation of WordPress on a CrystalTech shared windows hosting plan with IIS. Thanks!

  18. Ankit says

    June 11, 2008 at 12:17 am

    Hi, I am still facing a problem after doing et all… it comes back saying “The system cannot find the file specified.”… Can you help?

  19. mrkhm says

    June 19, 2008 at 1:33 pm

    oh my days! this was perfect i just did everything in 5 mins and it works, does this affect seo??

  20. Tom says

    June 20, 2008 at 9:41 am

    That’s a good question. Someone pointed out that they thought it would return a 404 error code to the search engines when it showed the “custom error page” that makes this solution work.

    If you test it out, you’ll see it doesn’t, however. It returns a 200 OK code in the response headers (you can test this with the Webmaster Tools extension for Firefox under Information -> View Response Headers)

    I’ve used this solution on many sites that have been search engine optimized and it works great and the search engines love it.

  21. Aloy says

    June 29, 2008 at 7:58 am

    awesome… it worked for me…. lovely… thanks a lot guys….

    at last i can start posting my poems… lovely 😀

    if u guys wanna read my first poem just jump in thr… and say if u like it or not…. 🙂
    criticism appreciated 🙂

  22. Jeff says

    July 3, 2008 at 11:45 am

    This permalink solution worked great for me (I’m also hosting at Crystaltech).

    One question, though. Is there still a way to handle -actual- 404 errors? My permalinks now work, but actual invalid urls just return a blank page.

    Thanks!

  23. Tom says

    July 3, 2008 at 1:48 pm

    Good question Jeff. I don’t have an easy answer to that. As far as I understand that logic would have to be done within the index.php page itself, and would complicate things.

  24. RJ says

    July 14, 2008 at 5:46 am

    hi,
    have a question.
    I have iis server.
    my site has been up for around a year now and using the default permalink
    namely http://www.yourdomain.com/?p=123

    I want to do permalink migration, the solution provided above is great for a new site on IIS, but doesn\’t it also work for permalink migration, i don\’t want to loose the SEO visitors, plz help, I m looking for a solution to migrate to a new permalink eg.

    /category/posttitle/

    and also retain my seo visitors, and i m using IIS .
    Thanks

  25. Tom says

    July 14, 2008 at 11:12 am

    Hi RJ,

    What you\’re looking for is a way to 301 redirect the pages the old pages to the new pages.

    I don\’t have any good ways of doing this other than just changing your link structure (which doesn\’t do anything for the 301 redirect issue).

    Your old pages should continue to come up at the old urls (?p=123), so you shouldn\’t lose any visitors, but it may take a while for the search engines to start indexing the new permalink urls.

    The best case scenario would be for someone to write a wp plugin that used the permalink structure that is stored in the wp database to do 301 redirects to the appropriate urls when the index.php was accessed directly. But that is quite a project all in itself.

    I hope that helps.

    Best,
    Tom

  26. annvremont says

    July 16, 2008 at 10:23 am

    Clint or Jeff or anyone else – I\’m using CT shared hosting, too (MySQL5) and the Revolution Pro Media Theme, have tried to implement but being such a newb am not sure I\’m doing it right. My theme has a 404.php file, hosted in the theme\’s folder. Do I create a new wp-404-handler.php file and delete the current 404.php file. Should the file have only the php text given above, should I integrate the php text above into the 404.php file instead? Should I put the wp-404-handler file somewhere other than under the folder for the theme I\’m using?

    I\’ve tried several of these variations and I just keep getting variations in the type of error I\’m getting (it started as just a blank page for any link that was /?p= — then trying to implement this, I\’ve had the cat links go crazy, too, the blank page replaced by fatal error messages, the basic \”page cannot be found\” and so on.

    (I posted this on the CP support forum, too, haven\’t been able to get my registration for the WP forum to stick, and have tried the revolution theme forums, too)

    Thanks!

  27. annvremont says

    July 16, 2008 at 10:23 am

    Oh, and am using 2.5.1 version of WP, if that is relevant.

  28. Randy says

    August 21, 2008 at 8:38 am

    If I set everything up as instructed and use /%pagename%/ I get this error: Parse error: syntax error, unexpected ‘:’ in \\NAWINFS04\home\users\web\b2850\rh.mediamngr\first_ky\wp-404-handler.php on line 3

    Because they are pages not posts?

  29. Randy says

    August 21, 2008 at 8:55 am

    Disregard my last I got it working, works great with pages as well.

    /%pagename%/.

    Thanks to Einar and Tom for sharing your efforts.

  30. Blackwings says

    August 27, 2008 at 1:51 pm

    I first setup your old ‘errorpage.asp’ solution and it worked fine, then found this and decided to test it. Now get HTTP 500 (internal server) errors on dedicated IIS 6.

    Going back to old version – for now.

  31. AIR says

    October 6, 2008 at 1:20 pm

    I think this is the easier solution for Permanlinks on IIS.
    Thank you so much whoever posted this code.

  32. Bernhard says

    November 15, 2008 at 5:10 am

    First i also had an “Parse error: syntax error, unexpected ‘:’ in /var/www/web441/html/wordpress_alt/wp-404-handler.php on line 3”

    But after i have included “/index.php/” in my structure: /index.php/%year%/%category%/ID%post_id%/%postname%.html”, everything seems working fine.

    thx for the wp-404-handler.

    Greetings from Austria

  33. Saad says

    January 27, 2009 at 8:54 am

    My website is “http://blogs.fun2sss.com

    Please tell urgent how I use ?

  34. Nat says

    February 24, 2009 at 9:38 pm

    Thank you so much for sharing this information. I am running WordPress in IIS6 and can have pretty URLs without installing any 3rd party software. (Step one, modified my php.ini, step two, followed your directions)

    Very slick and very easy.

    Please note that the code snippet in your post contains “smart” angled single quotes that had to be replaced with “dumb” vertical ones to make it work (for me):

    For example…
    “…($qs, ‘:80′)+3)” should be “…($qs, ‘:80′)+3)”
    “include(’index.php’);” should be “include(‘index.php’);”

  35. Nat says

    February 24, 2009 at 9:41 pm

    Yikes…just found out why your post has those smart quotes, the “correction” I just posted got its quotes automatically mangled when they were posted. Just make sure all the single quotes in the PHP code are replaced with generic dumb ones if you cut and paste the code.

  36. Robin says

    February 25, 2009 at 6:09 am

    Thank you, thank you, thank you for this! The only problem I am having is the categories URLs. Something is not working correctly and I am getting:

    http://www.domainname.com/blog/index.php/%year%/board-meetings/ID%post_id%/%postname%/board-meetings

    Any help will be greatly appreciated!

  37. Robin says

    February 25, 2009 at 6:15 am

    Found the problem… Had the Category base set wrong… Thanks again!!

  38. Wil says

    March 26, 2009 at 1:11 am

    This works for me as well, but seems to have messed up my feed, anyone else with that problem?

  39. Acer says

    March 29, 2009 at 11:48 pm

    Hmmm…I have my blog getting served up off of a subdirectory of a main site….so wordpress is installed in http://www.site1.com/wordpress, but my blog URL is http://www.site2.com.

    This fix works great if I set the URL to http://www.site1.com/wordpress, but never “finds” the right post if I set it to the http://www.site2.com.

    I’ll have to learn some PHP and play around to see if I can make it work, if you have any ideas, let me know!

    Thanks for the fix!

  40. pat says

    March 31, 2009 at 8:11 am

    I’m having the same problem with the blank page that several people mentioned. What was the fix for this? TIA

  41. Artanis says

    April 7, 2009 at 10:04 am

    First of all, thanks for your guide, It’s the only method that worked for me(after ~6h of try and error with many others), but I have a problem with the information and category links, if I follow those I end in a wp 404 error, apparently that happens with any link other than “?p”.

    Any idea of how to resolve that? I would really prefer avoid having to get into the wp code, but if necessary I have some experience with php and custom CMS.

    I’m using IIS5.1(and 6 too), wp 2.6.3 and cant change versions due to restrictions from my contractor.

    Thanks in advance.

  42. Christopher Doran says

    April 21, 2009 at 8:17 am

    Well I have to say I’m impressed too, I have it working sort of and just wondered If anyone had any ideas for the error I get.
    I am using 2.7.1 of wordpress on an II6 windows vps, when I set permalinks in wordpress to default the site work fine.. when I set it to any of the other options I get this error

    Warning: main(’indexphp’) [function.main]: failed to open stream: No such file or directory in C:\Inetpub\vhosts\christopherdoran.co.uk\httpdocs\wp-404-handler.php on line 5

    Warning: main() [function.include]: Failed opening ‘’indexphp’’ for inclusion (include_path=’.;./includes;./pear’) in C:\Inetpub\vhosts\christopherdoran.co.uk\httpdocs\wp-404-handler.php on line 5

    my wp-404-handler.php contains the following :

    Have I missed something silly ? all help greatly appreciated 🙂

  43. Christopher Doran says

    April 21, 2009 at 8:25 am

    fixed it using

  44. chewie says

    April 29, 2009 at 7:14 pm

    Tried to do this on my fresh WP install located here: http://blog.killthecan.org/

    Got the following message: No Input file specified.

    Thoughts?

  45. Tom says

    April 30, 2009 at 12:00 pm

    That error generally means that it couldn’t find the file specified. If you request a php file that doesn’t exist, it should give you that same error.

    For example:
    http://blog.killthecan.org/test.php

    Gives the same error.

    My guess is that you haven’t set up the 404 redirect correctly.

    Best,
    Tom

  46. Lewis says

    May 1, 2009 at 8:03 am

    you rock, thanks doode

  47. william says

    June 17, 2009 at 1:39 pm

    Thank you! I have been searching for a fix for this since wordpress 2.5!

    I ran into a few problems other people had listed. I’ll post a few and hopefully it will help someone

    My wordpress is in a folder called /site/. Godaddy hosting, windows dedicated server, iis 6.

    The wp-404-handler.php file goes in your wordpress root folder, not the theme folder.

    My “include” in the wp-404-handler.php file required the full site path. Ex: include ‘http://mysite.com/site/index.php’;
    Not having the full path gave me no error, just a blank page.

  48. Simon says

    July 31, 2009 at 3:33 pm

    Hi,

    I thought this was an excellent solution to this issue – I have it up and running just fine.

    Be aware though that as this currently stands it does prevent you from using $_POST and $_GET as Hep pointed out above. So if you have a contact form or something similar that requires this then this solution wont work for you.

    If you just use WordPress as it comes out of the box then this is great.

    Does anyone know of a way to enable form posting/reading functionality?

    Many thanks!

  49. Chad Killingsworth says

    September 6, 2009 at 8:49 pm

    I got the blank page at first as well. Eventually tracked it down to the fact that my web host had the default document set to “Index.php” with a capital “I”. Changing it to all lowercase seemed to fix the problem.

  50. Ivo says

    September 10, 2009 at 9:28 am

    Please help! I tried your handler workaround but the whole point is NOT to have index.php in my pretty permalinks. With index.php in there, they aren’t really pretty, are they? 🙂

    My custom permalink is /%year%/%monthnum%/%postname%/

    I erased the line 5 (include index.php) and it now gives me a blank page. Can someone please help me??? Thanks!

Newer Comments »

Leave a Reply Cancel reply

Your email address will not be published.

Pages

  • About Me
  • WordPress Permalinks in IIS using Custom 404 Redirect

Archives

  • May 2019
  • February 2019
  • January 2018
  • May 2016
  • April 2016
  • December 2015
  • November 2015
  • May 2015
  • April 2015
  • January 2013
  • March 2012
  • February 2012
  • December 2011
  • September 2011
  • August 2011
  • November 2010
  • October 2010
  • June 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • September 2009
  • August 2009
  • July 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • November 2008
  • September 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • December 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • January 2007
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • May 2005

ASP.NET Resources

  • Keven Roth’s Code Library
  • Lemon Law

Blogroll

  • Arin Morfopoulos
  • Jamie
  • Physical Therapists

Code Search Engines

  • Koders
  • Krugle

CSS & Design Resources

  • CSS Basics

Other

  • Amazon
  • Car Repair Questions
  • Cool Quizzes
  • Delphi LA
  • Physical Therapy Clinic Directory
  • Quickbooks Questions
  • Secret Santa Game

Site\'s I\'ve Designed or Helped With

  • Area Rugs
  • Dental Implants
  • Dental Website Design
  • FHA Loans
  • Secret Santa Gift Ideas