inexistent node dosn't redirect to error page
copernico - March 20, 2007 - 20:18
Hi, I'm new to drupal and just building my first site with it, I had a problem that I couldn't find any answer in the forum:
If I enter an URL like mysite.com/node/xxxx and the xxxx node does not exist, it redirects to the drupal default installation home page and not to the customized error page, it works with an unexistent path but not with a node number...
any ideas?
Thanks
Copernico

So you're saying
So you're saying mysite.com/node/xxxx doesn't work but /node/yyyy does?
Example
I'll give you an example:
mysite.com/somepage.html (somepage.html does not exist)
redirects to a an error 404 page which is correct.
mysite.com/node/999 (node 999 does not exist)
redirects to the installation default home page (the welcome to drupal)
/node/999 should redirect to the 404 page too because the node 999 is inexistant.
This is by design
If you use a URL alias and there is no associated node/x internal path, you get a No Page Found. While...
If you use an internal path that has arguments that lead nowhere, such as node/x where x is a non-existant node, you are led to the chopped internal path that works, i.e. node, hence the front page.
Another example, say I type this path : admin/chocolate-ice-cream, I will be led to : admin because admin/chocolate-ice-cream does not go anywhere but admin does...
Caroline
A coder's guide to file download in Drupal
Who am I | Where are we
11 heavens
this is a new "feature" as of drupal 5
and in my opinion not a good one.
for example, i have some sites where i've got something other than node as the front page. no nodes are promoted. so if you enter an invalid id in a node url, you get the default welcome page.
which is lame.
additionally, it's good practice to have a 404 page that gives your users some information about how to find the information they're looking for on the site. my custom 404 doesn't show up now for users who put in any kind of bad node url.
in general, a url that doesn't point to a page should return a 404.
i've never liked the default welcome page. there should at least be an option to turn it off and provide the 404 page instead. i guess i'll open an issue in the node project if there isn't one already for this.
found a solution
banged my head for a few hours trying to solve this and found a quite simple solution: override the menu entry from the node module in your module.
in function yourModule_menu() add this menu entry:
<?php$items[] = array('path' => 'node',
'callback' => 'override_node_page_default',
'access' => TRUE,
'type' => MENU_CALLBACK);
?>
and the override function can do whatever you want (render a page, redirect, etc.). Example of a custom 404:
<?php
function override_node_page_default(){
$title = '<div class="message_title">' . t('404 - Page Not Found') . '</div>';
$body = '<div class="message_body">' . t('The page you have requested does not exist. Please check if the you URL have typed is correct.') . '</div>';
drupal_set_title($title);
print theme('page', $body);
exit();
?>
I have tested it and it seems to work just fine, existing nodes are displayed correctly, non-existing show the default 404.
Perfect.
Awesome, thanks thestgman!!!
Yes, this is the intended behavior for Drupal 5, but it's very silly IMNSHO. Drupal is great at being fast, secure and having lots of 3rd party modules, but it fails at things like this and usability. I guess that's what happens when you have people who are engineers/coders and no one being able to guide the project and enforce some human interface guidelines.
Buuuuuuut I digress --- this is a great fix!
Another approach
After always hacking that pointles frontpage (that gives a total listing instead of 404) I got used to changing each default not to publish on front page - leaving us with the welcom message as a 404. Then it suddenly struck me - change the 404 page node (just that node) to be published on frontpage - an voila!
You get a 404 page!