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(&$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?

  • $node->nid is the invalid nid assuming all the rest of your code is valid.
    – Jimajamma
    Commented Oct 17, 2012 at 15:53
  • It was the problem the node->nid return the nid of the 404 page, not the product nid. Commented Oct 18, 2012 at 9:33
  • gotcha...I got snagged up in the 'node' actually being not empty logic :(
    – Jimajamma
    Commented Oct 18, 2012 at 17:30

1 Answer 1


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") {
 } else {
  print $pageURL; 

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.