Pragmatism in code

If you want to replace the default Joomla behaviour of showing a 404 error for restricted content, then you can use the follow custom error handler within your template.

As no core files are modified, and only template changes are made, this is a 'legal' enhancement. Feel free to modify the code to do as you wish.

Simply replace the contents of your error.php file with the following...

 * @version     $Id: error.php 17282 2010-05-26 15:24:49Z infograf768 $
 * @package     Joomla.Site
 * @copyright   Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt

defined('_JEXEC') or die;
$params = JFactory::getApplication()->getTemplate(true)->params;
$logo =  $params->get('logo');
$showRightColumn = 0;
$showleft = 0;
$showbottom = 0;

// get params
$color              = $params->get('templatecolor');
$navposition        = $params->get('navposition');

$templateparams =  JFactory::getApplication()->getTemplate(true)->params;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" xml:lang="<?php echo $this->language; ?>" lang="<?php echo $this->language; ?>" dir="<?php echo $this->direction; ?>">
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<?php  $doc=JFactory::getDocument();
<meta name="language" content="<?php echo $language; ?>" />

    <title><?php echo $this->error->getCode(); ?> - <?php echo $this->title; ?></title>
<?php if ($this->error->getCode()>=400 && $this->error->getCode() < 500) {  ?>

        <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/system.css" type="text/css" />
        <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/ohsadmin/css/position.css" type="text/css" media="screen,projection" />
        <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/ohsadmin/css/layout.css" type="text/css" media="screen,projection" />
        <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/ohsadmin/css/print.css" type="text/css" media="Print" />
        <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/ohsadmin/css/<?php echo htmlspecialchars($color); ?>.css" type="text/css" />     
    $files = JHtml::_('stylesheet','templates/ohsadmin/css/general.css',null,false,true);
    if ($files):
        if (!is_array($files)):
            $files = array($files);
        foreach($files as $file):
        <link rel="stylesheet" href="<?php echo $file;?>" type="text/css" />
        <?php if ($this->direction == 'rtl') : ?>
        <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/ohsadmin/css/template_rtl.css" type="text/css" />
        <?php endif; ?>
        <!--[if lte IE 6]>
            <link href="/<?php echo $this->baseurl ?>/templates/ohsadmin/css/ieonly.css" rel="stylesheet" type="text/css" />
        <!--[if IE 7]>
            <link href="/<?php echo $this->baseurl ?>/templates/ohsadmin/css/ie7only.css" rel="stylesheet" type="text/css" />
<style type="text/css">
            #errorboxbody h2
            border:solid 1px #ddd



    <div id="all">
        <div id="back">

        <div id="header">
                    <div class="logoheader">
                                $params = JFactory::getApplication()->getTemplate(true)->params;
                                $logo =  $params->get('logo'); 
                            <?php jimport( 'joomla.application.module.helper' ); ?>

                         <h1 id="logo">

                                        <?php if ($logo): ?>
                                        <img src="<?php echo $this->baseurl ?>/<?php echo htmlspecialchars($logo); ?>"  alt="<?php echo htmlspecialchars($params->get('sitetitle'));?>" />
                                        <?php endif;?>
                                        <?php if (!$logo ): ?>
                                        <?php echo htmlspecialchars($params->get('sitetitle'));?>
                                        <?php endif; ?>
                                        <span class="header1">
                                        <?php echo htmlspecialchars($params->get('sitedescription'));?>
                    </div><!-- end logoheader -->
                        <ul class="skiplinks">
                            <li><a href="#wrapper2" class="u2"><?php echo JText::_('TPL_OHSADMIN_SKIP_TO_ERROR_CONTENT'); ?></a></li>
                            <li><a href="#nav" class="u2"><?php echo JText::_('TPL_OHSADMIN_ERROR_JUMP_TO_NAV'); ?></a></li>
                        <div id="line"></div>
                </div><!-- end header -->
        <div id="contentarea2" >
                            <div class="left1" id="nav">
                <h2 class="unseen"><?php echo JText::_('TPL_OHSADMIN_NAVIGATION'); ?></h2>
                                            <?php $module = JModuleHelper::getModule( 'menu' );
                                            echo JModuleHelper::renderModule( $module); ?>
                    <!-- end navi -->

            <!--//DeeEmm[authentication time out 404 error]-->
            // Make sure the error is a 404 and we are not in the administrator.
            $user =& JFactory::getUser();
            //page is not found and user is not logged in
            if ( ($this->error->getCode() == 404) and ($user->get('id') == '0'))
            // Get the application object.
            $app = JFactory::getApplication();
            //Determine if there is an authorised page that requires login to view
            // Get the full current URI.
            $uri    = clone JURI::getInstance();
            $current = $uri->toString(array('scheme', 'host', 'port', 'path', 'query', 'fragment'));

            // See if the current url exists in the database as a redirect.
            $db = JFactory::getDBO();
                'SELECT count(*) as possibleMatches FROM `#__menu` as m WHERE m.access > 1 AND m.path IS NOT NULL AND m.path != "" AND "'.$uri->getPath().'" LIKE concat("%/",m.path,"%")'
            $links = $db->loadObject();
            //print_r($links);  die((string)$links);
                //redirect to the login form, with return = current url
                $url = 'index.php?option=com_users&view=login&return='.base64_encode($current);
                $urlRedirect = JRoute::_($url);
                $app->redirect($urlRedirect, $msg=$params->get('message'), $msgType='message');
                return false;
            //hand off to the standard redirect plugin
            } else {

            <div id="wrapper2">
            <div id="errorboxbody">
                        <h2><?php echo JText::_('JERROR_AN_ERROR_HAS_OCCURRED'); ?><br />
                                <?php echo JText::_('JERROR_LAYOUT_PAGE_NOT_FOUND'); ?></h2>
                                            <div id="searchbox">
                                            <h3 class="unseen"><?php echo JText::_('TPL_OHSADMIN_SEARCH'); ?></h3>
                                            <p><?php echo JText::_('JERROR_LAYOUT_SEARCH'); ?></p>
                                            <?php $module = JModuleHelper::getModule( 'search' );
                                            echo JModuleHelper::renderModule( $module); ?>
                                                <p><a href="<?php echo $this->baseurl . ' ' ; ?>/index.php" title="<?php echo JText::_('JERROR_LAYOUT_GO_TO_THE_HOME_PAGE'); ?>"><?php echo JText::_('JERROR_LAYOUT_HOME_PAGE'); ?></a></p> 
                                    <p><?php echo $this->error->getCode() ; echo $this->error->getMessage();?><br />
                    <?php echo JText::_('JERROR_LAYOUT_PLEASE_CONTACT_THE_SYSTEM_ADMINISTRATOR'); ?>.</p>
                </div><!-- end wrapper -->  
                <?php } ?>
                <!--//DeeEmm[authentication time out 404 error]-->

            </div><!-- end contentarea -->

                        <?php if ($this->debug) :
                            echo $this->renderBacktrace();
                        endif; ?>

            </div>  <!--end all -->
             <div id="footer-outer">
            <div id="footer-sub">

                                <div id="footer">
                                           <a style="text-decoration:none" href="">Indau.Pty.Ltd</a> ABN 51 008 182 161 

                                </div><!-- end footer -->
                         </div><!-- end footer-sub -->

<?php } else { ?>
if (!isset($this->error)) {
    $this->error = JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR'));
    $this->debug = false; 
    <link rel="stylesheet" href="<?php echo $this->baseurl; ?>/templates/system/css/error.css" type="text/css" />
    <div class="error">
        <div id="outline">
        <div id="errorboxoutline">
            <div id="errorboxheader"> <?php echo $this->title; ?></div>
            <div id="errorboxbody">
            <p><strong><?php echo JText::_('JERROR_LAYOUT_NOT_ABLE_TO_VISIT'); ?></strong></p>
                    <li><?php echo JText::_('JERROR_LAYOUT_AN_OUT_OF_DATE_BOOKMARK_FAVOURITE'); ?></li>
                    <li><?php echo JText::_('JERROR_LAYOUT_SEARCH_ENGINE_OUT_OF_DATE_LISTING'); ?></li>
                    <li><?php echo JText::_('JERROR_LAYOUT_MIS_TYPED_ADDRESS'); ?></li>
                    <li><?php echo JText::_('JERROR_LAYOUT_YOU_HAVE_NO_ACCESS_TO_THIS_PAGE'); ?></li>
                    <li><?php echo JText::_('JERROR_LAYOUT_REQUESTED_RESOURCE_WAS_NOT_FOUND'); ?></li>
                    <li><?php echo JText::_('JERROR_LAYOUT_ERROR_HAS_OCCURRED_WHILE_PROCESSING_YOUR_REQUEST'); ?></li>
            <p><strong><?php echo JText::_('JERROR_LAYOUT_PLEASE_TRY_ONE_OF_THE_FOLLOWING_PAGES'); ?></strong></p>

                    <li><a href="<?php echo $this->baseurl; ?>/index.php" title="<?php echo JText::_('JERROR_LAYOUT_GO_TO_THE_HOME_PAGE'); ?>"><?php echo JText::_('JERROR_LAYOUT_HOME_PAGE'); ?></a></li>
                    <li><a href="<?php echo $this->baseurl; ?>/index.php?option=com_search" title="<?php echo JText::_('JERROR_LAYOUT_SEARCH_PAGE'); ?>"><?php echo JText::_('JERROR_LAYOUT_SEARCH_PAGE'); ?></a></li>


            <p><?php echo JText::_('JERROR_LAYOUT_PLEASE_CONTACT_THE_SYSTEM_ADMINISTRATOR'); ?>.</p>
            <div id="techinfo">
            <p><?php echo $this->error->getMessage(); ?></p>
                <?php if ($this->debug) :
                    echo $this->renderBacktrace();
                endif; ?>

<?php } ?>

The modification is an adaptation of the advanced login redirect on 404 mod by Chad Wagner -