Sep
07
2007

Update for WordPress Permalinks on IIS

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.

Share and Enjoy:
  • del.icio.us
  • digg
  • Furl
  • NewsVine
  • RawSugar
  • Reddit
  • Spurl
  • YahooMyWeb
Written by Tom in: Installing Wordpress |

56 Comments »

  • 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!

    Comment | October 26, 2007
  • P.S.

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

    Comment | October 26, 2007
  • Tom

    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.

    Comment | October 26, 2007
  • Thanks again!

    Comment | October 27, 2007
  • Luke

    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?

    Comment | November 1, 2007
  • Hi, do you know if that will work with wordpress mu?

    Comment | November 15, 2007
  • rob

    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?

    Comment | December 5, 2007
  • My Permalinks Include The “index.php”
    But I dislike T-T

    Comment | December 18, 2007
  • Hep

    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…

    Comment | January 8, 2008
  • Thankyou so much! I have been looking for something like this for ages and you are the first place that has a working answer!

    Comment | January 14, 2008
  • 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.

    Comment | February 17, 2008
  • Thank you so much for this! It works awesome!

    Comment | April 2, 2008
  • 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?

    Comment | April 12, 2008
  • THANKS! WORKS PERFECTLY!

    Comment | May 30, 2008
  • 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.

    Comment | June 9, 2008
  • Tom

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

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

    Comment | June 10, 2008
  • 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?

    Comment | June 11, 2008
  • mrkhm

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

    Comment | June 19, 2008
  • Tom

    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.

    Comment | June 20, 2008
  • awesome… it worked for me…. lovely… thanks a lot guys….

    at last i can start posting my poems… lovely :D

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

    Comment | June 29, 2008
  • Jeff

    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!

    Comment | July 3, 2008
  • Tom

    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.

    Comment | July 3, 2008
  • RJ

    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

    Comment | July 14, 2008
  • Tom

    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

    Comment | July 14, 2008
  • 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!

    Comment | July 16, 2008
  • Oh, and am using 2.5.1 version of WP, if that is relevant.

    Comment | July 16, 2008
  • 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?

    Comment | August 21, 2008
  • Disregard my last I got it working, works great with pages as well.

    /%pagename%/.

    Thanks to Einar and Tom for sharing your efforts.

    Comment | August 21, 2008
  • 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.

    Comment | August 27, 2008
  • AIR

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

    Comment | October 6, 2008
  • 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

    Comment | November 15, 2008
  • Saad

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

    Please tell urgent how I use ?

    Comment | January 27, 2009
  • Nat

    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’);”

    Comment | February 24, 2009
  • Nat

    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.

    Comment | February 24, 2009
  • 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!

    Comment | February 25, 2009
  • Found the problem… Had the Category base set wrong… Thanks again!!

    Comment | February 25, 2009
  • Wil

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

    Comment | March 26, 2009
  • Acer

    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!

    Comment | March 29, 2009
  • pat

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

    Comment | March 31, 2009
  • Artanis

    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.

    Comment | April 7, 2009
  • 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 :-)

    Comment | April 21, 2009
  • fixed it using

    Comment | April 21, 2009
  • Tried to do this on my fresh WP install located here: http://blog.killthecan.org/

    Got the following message: No Input file specified.

    Thoughts?

    Comment | April 29, 2009
  • Tom

    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

    Comment | April 30, 2009
  • you rock, thanks doode

    Comment | May 1, 2009
  • 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.

    Comment | June 17, 2009
  • Simon

    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!

    Comment | July 31, 2009
  • Chad Killingsworth

    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.

    Comment | September 6, 2009
  • Ivo

    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!

    Comment | September 10, 2009
  • Ivo

    Scratch my previous cry for help, Einar helped me out with his post. Now the problem is that any other 404 on my whole website (that is not my blog) is a blank page. Anyone have a solution for this?

    blog: http://www.seabrookwa.com/blog
    website: http://www.seabrookwa.com

    THANKS!

    Comment | September 10, 2009
  • Ivo

    I followed your instructions and it worked. However, now the blog’s main page shows me my sidebar while the individual post pages only have the post text and not the sidebar.

    Any ideas on why that is?

    THanks!

    Comment | September 10, 2009
  • hi thanks for the code, it worked like a charm!

    i want to know if its possible to write a custom 404 page which can handle bbpress permalinks as well as wordpress permalinks in the same 404 file. iam running the forum at mywebsite/forum

    thanks

    Comment | September 13, 2009
  • Custom 404′s while preserving the ability to have permalinks are possible using this hack with a small tweak — see http://dotnetyuppie.com/2009/09/21/permalinks-and-mod_rewrite-headaches-in-wordpress-with-iis/

    Comment | September 21, 2009
  • “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.”

    Has anyone any idea how to make this work. I need to use qTranslate, and this requires me to use ?Lang=TR

    Comment | December 11, 2009
  • Thanks a ton for this solution. I had seraching a lot for it and now I am using it in WordPress thesis theme with IIS. HHowever I have a problem, when I go to individual post the page is refreshing again and again. I am not able to resolve it. Can someone hwlp please?. My Blog is for a tour operator in India @ http://www.irisholidays.com/blog/volunteering-vacations-in-kerala/2010/03/05/

    Comment | March 5, 2010

RSS feed for comments on this post.

Leave a comment

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes