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.
Sal B says
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!
Sal B says
P.S.
Is this a bot killer?? IE – will I lose traffic due to this? Please email me about this.
Tom says
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.
Sal B says
Thanks again!
Luke says
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?
Will says
Hi, do you know if that will work with wordpress mu?
rob says
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?
5ivedance says
My Permalinks Include The “index.php”
But I dislike T-T
Hep says
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…
Thomas Sinfield says
Thankyou so much! I have been looking for something like this for ages and you are the first place that has a working answer!
Lazlow says
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.
Mike Boudet says
Thank you so much for this! It works awesome!
Diana says
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?
Mike says
THANKS! WORKS PERFECTLY!
Clint says
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.
Tom says
Thanks! That’s a good catch. I updated it.
Clint says
NP. Thanks for posting this, this solution worked great for my installation of WordPress on a CrystalTech shared windows hosting plan with IIS. Thanks!
Ankit says
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?
mrkhm says
oh my days! this was perfect i just did everything in 5 mins and it works, does this affect seo??
Tom says
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.
Aloy says
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 🙂
Jeff says
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!
Tom says
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.
RJ says
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
Tom says
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
annvremont says
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!
annvremont says
Oh, and am using 2.5.1 version of WP, if that is relevant.
Randy says
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?
Randy says
Disregard my last I got it working, works great with pages as well.
/%pagename%/.
Thanks to Einar and Tom for sharing your efforts.
Blackwings says
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.
AIR says
I think this is the easier solution for Permanlinks on IIS.
Thank you so much whoever posted this code.
Bernhard says
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
Saad says
My website is “http://blogs.fun2sss.com
Please tell urgent how I use ?
Nat says
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’);”
Nat says
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.
Robin says
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!
Robin says
Found the problem… Had the Category base set wrong… Thanks again!!
Wil says
This works for me as well, but seems to have messed up my feed, anyone else with that problem?
Acer says
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!
pat says
I’m having the same problem with the blank page that several people mentioned. What was the fix for this? TIA
Artanis says
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.
Christopher Doran says
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 🙂
Christopher Doran says
fixed it using
chewie says
Tried to do this on my fresh WP install located here: http://blog.killthecan.org/
Got the following message: No Input file specified.
Thoughts?
Tom says
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
Lewis says
you rock, thanks doode
william says
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.
Simon says
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!
Chad Killingsworth says
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.
Ivo says
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!