How to redirect anonymous user to 404 instead of 403? I'm new to Drupal so am struggling here. I've created a checkbox field and the goals is that when it is checked it redirects anonymous to the 404 page.

I have the following condition:

function page_module_preprocess_page(&amp;$variables) {

 // code to retrive checkbox value

 if (user_is_anonymous() &amp;&amp; $page_hidden_value == 1) {
    drupal_not_found();
  }
}


But this doesnt work.

However, interestingly enough if I do the reverse and check that the user is logged in (!user_is_anonymous()) it does work and does redirect to 404.

I can DPM the value of the checkbox ($page_hidden_value) so I know that's working correctly.

Would anyone know what could be going on here?
I have had some odd experiences with user_is_anonymous(). Try using negation with user_is_logged_in(). For example:

if (!user_is_logged_in()) {
    // Do stuff
}