Please Help with Clean URL's

Needing some assistance in setting up drupal to work with clean url's on IIS 6.0.

I am using IIS 6.0 on a shared hosted server - Using Drupal 6.6

I have currently setup my 404 redirect to direct back to my index.asp

The code that I have come up with is along the lines of this

if (isset($_SERVER['QUERY_STRING'])) {
  //break down url
  $qs = explode(";", $_SERVER["QUERY_STRING"]);
  //only do if attempting clean url's
  if ($qs['0'] == 404) {
    $parts = parse_url($qs['1']);
    if ($parts['path'] != $_SERVER['SCRIPT_NAME']) {
      //take off any trailing /
      $parts['path'] = trim($parts['path'], "/");
      //check to see if there is a query string in addition to clean url's
      if ($parts['query'] != "") {
        $parts['path'] = $parts['path'] . "&" . $parts['query'];
      //return clean path and query string as query string
      $uri = $_SERVER['SCRIPT_NAME'] . '?q=' . ($parts['path']);
    else {
      //if we are at the index page but bad query string
      $uri = variable_get('site_404', '');
  else {
    //if we are at a valid page and valid query string
    $uri = $_SERVER['SCRIPT_NAME'] .'?'. $_SERVER['QUERY_STRING'];

Now versions of this bit of code works on most of my test scripts that I made to utilize it but I am not sure where to implement this code in drupal to have drupal use it. I have tried

Please do not tell me to change or do anything physical. IE change hosts or servers etc.
I can change my scripts or modify drupal in anyway that is required though.

I am currently going to be stuck on an IIS 6.0 shared hosting server.

Thanks to anyone in advance,


rlandersen’s picture

ok I manage to figure it out, utilizing the original code found here

$sub_dir = "/41/"; // enter a subdirectory, if any. otherwise, use ""
$active = 0; // set to 1 if using clean URLS with IIS

if ($active && strstr($_SERVER["QUERY_STRING"], ";")) {
  $qs = explode(";", $_SERVER["QUERY_STRING"]);
  $url = array_pop($qs);
  $parts = parse_url($url);
  unset($_GET, $_SERVER['QUERY_STRING']); // remove cruft added by IIS
  if ($sub_dir) {
    $parts["path"] = substr($parts["path"], strlen($sub_dir));
  $_GET["q"] = trim($parts["path"], "/");
  $_SERVER["REQUEST_URI"] = $parts["path"];
  if ($parts["query"]) {
    $_SERVER["REQUEST_URI"] .= '?'. $parts["query"]; 
    $_SERVER["QUERY_STRING"] = $parts["query"];
    $_SERVER["ARGV"] = array($parts["query"]);
    parse_str($parts['query'], $arr);
    $_GET = array_merge($_GET, $arr);
    $_REQUEST = array_merge($_REQUEST, $arr);

When I first tried to use this code it would never work. I must have lost some brain cells or something. :)

I played around with other things and this time for some reason it started working, not sure it was just because of this alone or because of a combination of other things I was doing, Because I now have this code put in and my original code in. At this time it is working, Ya. :)

newbuntu’s picture

What's your final code that made it work? I used the code from the post

Everything else appears to be working, but I can't create or edit post :(

After I edit a post I see part of URL is repeated like: "mysite/node/1/node/1/edit" and "mysite/node/add/node/add/page"

so it must have something to do with that piece of php code...

ranaonline’s picture

I have the site at so the first line has $sub_dir = "/site/";.

*the code at*

Earlier I had the same problem. However, at that time my pages were also names better (with the page title in the link instead of node number). Now, my pages do not have the title info added to them and the problem of double link has also ended.
I am still trying to look for the exact answer to the problem, will post it again once I am able to figure out.


I checked and you are correct. The reason as I see it is that whenever we are trying to post (or change the settings or update any other part of the site) there is an interim URL - POSTURL?destination=URL. This gets appended in the friendly URL and therefore whenever any information is posted in the system, it starts to give trouble.

It is true that if you create your (static) site and then enable friendly URL then there will be no issues, as your users will be able to view the website with friendly urls. If you need to change any page, you can disable the friendly URLs and do it. I know its not very nice, but I am not expecting that you will host a large portal from a shared windows hosting account.

jbeall’s picture

I'm guessing you're having problems with all your forms, not just the create and edit forms?

If so, it's probably related to the problem that I noted about in this comment:

Namely, that IIS is stripping out POST variables as a part of it's redirection to your custom 404 handler.

But I was able to figure out a workaround for this, take a look at my code in the comment I linked to above, and let me know if it works for you.

tallmark515’s picture

This works for me too with Drupal 5 on IIS 6.

Good work