Skip to content

URL Mod-Rewrite Workaround for IIS 6.0 and WordPress

by John on September 15th, 2009

I found that the existing solution for using pretty permalinks with WordPress and IIS is good

and lightweight, but I needed something a little more robust that fixed as many differences as possible between Apache and IIS.

This code uses the same idea as the above, but fixes more values in _SERVER and resolves a problem with _GET. It will also work with later versions (2.8+) of WordPress.

<?php
// This is the default file for the site. Usually index.php
$default = 'index.php';

// The name of this file.
// Set this value for the URL in Custom Error Properties of your website in IIS.
// Goto: IIS Manager > Websites > [Site Name] > Properties > Custom Errors >
// 404 & 404;2 & 404;3 > URL (Requires a '/' prefix in IIS).
$thisfile = '404-handler.php';

$_SERVER['ORIG_PATH_TRANSLATED'] = str_replace($thisfile, $default, $_SERVER['ORIG_PATH_TRANSLATED']);
$_SERVER['SCRIPT_FILENAME'] = str_replace($thisfile, $default, $_SERVER['SCRIPT_FILENAME']);
$_SERVER['ORIG_PATH_INFO'] = str_replace($thisfile, $default, $_SERVER['ORIG_PATH_INFO']);
$_SERVER['SCRIPT_NAME'] = str_replace($thisfile, $default, $_SERVER['SCRIPT_NAME']);
$_SERVER['PHP_SELF'] = str_replace($thisfile, $default, $_SERVER['PHP_SELF']);
$_SERVER['PATH_INFO'] = false;

$qs =& $_SERVER['QUERY_STRING'];
$ru =& $_SERVER['REQUEST_URI'];
$pos = strrpos($qs, '://');
$pos = strpos($qs, '/', $pos + 4);
$_SERVER['URL'] = $ru = substr($qs, $pos);
$qs = trim(stristr($ru, '?'), '?');

// Required for WordPress 2.8+
$_SERVER['HTTP_X_ORIGINAL_URL'] = $ru;

// Fix GET vars
foreach ( $_GET as $var => $val ) {
  if ( substr($var, 0, 3) == '404') {
    if ( strstr($var, '?') ) {
      $newvar = substr($var, strpos($var, '?') + 1);
      $_GET[$newvar] = $val;
    }
    unset($_GET[$var]);
  }
  break;
}
include($default);
?>

From → Development

119 Comments
  1. You are an absolute genius. This seems to be the only thing that worked for me.
    However, the only problem I’m having is that the Flash application I have on my homepage stops working. Why do you think it does this, and how do I fix it?

  2. Thanks for this solution! Is there a way for actual 404s to redirect to a page using the 404.php template in my theme directory? Currently invalid URLs that would normally return a 404 show up blank.

  3. You are a lifesaver, I used this for a client that had Windows Hosting and insisted on a wordpress website design. The permalink structure for it was horrible and I know would ruin his search engine optimization strategy of having nice links. Thanks again for this great tutorial!

  4. it is very helpfull!thanks

  5. very helpful and useful with me, thanks for tip !

    Dan

  6. This worked a treat. I had no idea that WordPress could work on a Windows machine.

  7. Won’t this cause all pages pages deeper than the homepage to return a 404 status?

    This would be horrible from an SEO perspective!

    Please advise.

  8. nyckidd permalink

    Awesome dude!!!!

  9. omg thank you! I couldn’t get permalinks working for my new landing pages – found out it’s on IIS6 :P

  10. The code is work on my site, but there is a problem with the feed http://www.crusherinc.com/News/feed

    How to solve it?

  11. I did everything like the article says, but still, after changing the perrmalinks configuration, and setting the wp-404-handler file, I am getting the “page not found” error.

  12. Bruce permalink

    In Windows Server 2000 (IIS 5) running PHP 5, I was seeing the following message: “PHP Notice: Undefined index: ORIG_PATH_TRANSLATED in wp-404-handler.php on line 11″.

    I modified line 11 to:

    $_SERVER['PATH_TRANSLATED'] = str_replace($thisfile, $default, $_SERVER['PATH_TRANSLATED']);

    and that seems to have fixed the error.

    In other words, change ORIG_PATH_TRANSLATED to PATH_TRANSLATED.

  13. it is very helpfull, Thanks :)

  14. While there are actually many details of tree services which unfortunately I want to
    study, I appreciate that which you have outlined in this article,
    URL Mod-Rewrite Workaround for IIS 6.0 and WordPress | ikailo, as it truly helps me get
    a deeper understanding.

  15. Steve permalink

    I was able to resolve the issue with your suggested changes. Great work!!

    OS: Windows 2003
    WordPress: 3.5

  16. I do agree with all of the ideas you’ve introduced for your post. They are very convincing and can certainly work. Nonetheless, the posts are too brief for newbies. May you please lengthen them a little from next time? Thanks for the post.

  17. Good info. Lucky me I came across your blog
    by chance (stumbleupon). I have bookmarked it for later!

  18. I’ve been surfing online more than 2 hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me.

    In my opinion, if all website owners and bloggers made good content as you did, the web will be much more useful than ever before.

  19. I am regular visitor, how are you everybody? This paragraph posted at this website is
    actually good.

  20. I am really enjoying the theme/design of your web site.
    Do you ever run into any browser compatibility issues?
    A handful of my blog visitors have complained about my site not operating correctly
    in Explorer but looks great in Chrome. Do you have any advice to help fix this problem?

Trackbacks & Pingbacks

  1. Pretty Wordpress Permalinks on IIS » tech.einaregilsson.com
  2. Permalinks and mod_rewrite Headaches in Wordpress with IIS | Dot Net Yuppie
  3. Brian On Software » Blog Archive » Migrating a Wordpress site from BlueHost to WebHost4Life
  4. sawathee » Blog Archive » Using Permalinks
  5. Permalinks on a GoDaddy WIMP setup « 49thLatitude
  6. windows+IIS下完美设置wordpress伪静态 | 陈方毅的灵感日志
  7. Để không có ký tự tiếng việt ở url wordpress (Nicer permalinks for Vietnamese) | Script Online Collection - Free nulled script - Clone sites
  8. WordPress Pretty Permalinks on IIS | danlefeb.com
  9. 使用wordpress的固定连接 | Just For Lee
  10. 《创想曲》电子杂志
  11. 01博客 » 修改固定链接说明
  12. WordPress 固定链接设置方式 | 诺哥
  13. 使用WordPress固定链接 « Escope
  14. 使用WordPress固定链接 – 对比----想象
  15. Why Use WordPress on Windows Server 2003?
  16. 使用固定链接 | Citi
  17. Remove Wordpress Index.php on Windows Servers | No Joke Guide
  18. windows+IIS下完美设置wordpress伪静态 - 文文策划
  19. 【转】WordPress固定链接的设置方法 | 田园牧歌
  20. 10 Steps to Making Wordpress Permalinks Work on Windows IIS 6.0 – Web Coach Pete SEO Blog
  21. 使用WordPress固定链接 « KingChine
  22. 使用WordPress固定链接 - 来自深圳SEO QQ61149538 - 外贸SEO-B2C网站优化-专注SEO数据、SEO工具、SEO外链资源等
  23. Wordpress使用固定链接及出现问题解决办法
  24. 使用WordPress固定链接 | 钥匙博客
  25. jcamweb's blog » El blog en iis 6.0 como ejecutable cgi (parte 2)
  26. win系统IIS6.0情况下|去掉固定链接中的index.php | 爱睿智.com|睿之iruizhi.com
  27. Day Two of WordPress uh…. pressure…. | What if?
  28. URL Rewriting with WordPress — Visual Basic Feng Shui
  29. 使用WordPress固定链接 | AngKaKa | 分享我所知道的···
  30. wordpress使用固定链接 « 运维之路
  31. Pretty Permalinks, Wordpress, and EasyCGI | ChadCardwell.net
  32. wordpress 中进行伪静态设置的官方简体中文文档 « zhuohang
  33. nojokeguide.com » Remove Wordpress Index.php on Windows Servers
  34. wordpress使用固定鏈接 - ORZ部落閣
  35. 帮一把? | 帮一把
  36. wordpress:使用固定链接 | 快乐不快乐
  37. WordPress伪静态的实现 | 阿辉博客
  38. win主机下WP固定链接(伪静态)实现方法全攻略 | 阿辉博客
  39. wordpress固定连接设置 | 无忧建站|网站建设|网站推广|
  40. WordPress固定链接设置 | HHDZL
  41. 如何设置固定链接?WordPress固定链接的设置方法 - 中文吧
  42. 使用 WordPress 的固定链接 - Xu Design
  43. 使用固定链接
  44. WordPress固定链接的设置方法 | 梦之翼'Blog
  45. Test10000中文显示价格区_全球优选
  46. WordPress固定链接的设置方法 让Nginx支持WordPress的固定链接 | 新的网
  47. wordpress后台使用固定链接的方法 | 龚磊博客
  48. IIS服务器解决伪静态固定链接的一种完美解决方案 | 绝妙网

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS