Get the invalid URL which throw a 404 I have a catalog of products in one of my website, each year there are new products and some old products are unpublish (never deleted). Some websites put links to the products, so when a product is unpublish, all those referents links becomes invalid and are redirect to a 404 pages.

Now i don't want that, i want a custom redirection to parent category of the product. For that i catch the 404 event, and then i tough i could just make a dbquery on my node items to found the parent's category, and create a redirection.

I catch the 404 in the template.php file like that : 

 function myTheme_preprocess_node(&amp;$variables) {
  $node = $variables['node'];
  if (!empty($node)) {
    $headers = drupal_get_headers();
    if (preg_match('@HTTP/1\.[01]\x20+404@', $headers)) {
      //want to do my redirection here

    }
  }


but when i try to get my url, i get in fact the url of the 404 pages but in the url bar, it's still the product url.

So how can i get that url or simply the invalid nid?
My bad, i had one of those blind drupal moment :)

Yes it seem that drupal can't find the url because of the 404 which become the current node, but with some PHP we can still get it.

  $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
  print $pageURL; 


[that is not my code, so thank you Google.]