After noticing that the error pages for the site I had been working on didn't show any useful information, I decided to borrow some PHP code I had found for making my own site's error pages more useful:

<p>You were looking for:<br />
	   
	  <?php
	   $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
	   
	   if (!empty($_SERVER["QUERY_STRING"])) 
	     $url .= "?".$_SERVER['QUERY_STRING'];
	     
		 echo "<strong>".$url."</strong>";
	  ?>
 </p>

Unfortunately, this code doesn't work properly with Drupal - it adds "?q=", followed by the path (anything after the first single slash), to the end of the address. In other words, an address like:

http://example.com/does/not/exist

Would be displayed as:

http://example.com/does/not/exist?q=does/not/exist

Is there any way to modify this code to show the actual address, without adding "?q=" or duplicating the path?

Comments

Wolfey’s picture

It looks like this can be done: just compare the path (excluding the leading slash) and query string (excluding the "?q=" part) and see if they are identical. If so, do not show the query string, since it would be a duplicate path in this case:

<p>You were looking for:<br />
  
  <?php
       $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
       $site_path = substr($_SERVER['REQUEST_URI'],1);
       $site_query = substr($_SERVER['QUERY_STRING'],2);
       
       if (!empty($_SERVER["QUERY_STRING"]) && ($site_path != $site_query)) {
         $url .= "?".$_SERVER['QUERY_STRING'];
       }
       
       echo "<strong>".$url."</strong>";
  ?>
  
</p>

It works as expected =)

If an address intentionally had an identical path and query string, this could be a problem...but, from what I have seen, something like that seems very unlikely - even if it could happen, the code above could be modified to take that case into account.