Jump to content



Photo

Ch02: Custom Error Page-Error404

ch02 error page

  • Please log in to reply
17 replies to this topic

#1 andrey

andrey

    Newbie

  • Members
  • Pip
  • 10 posts

Posted 31 December 2016 - 7:02 AM

Hello everyone!

I try to make a error page based on Larry code. but encountered a problem. There was a Problem with a redirect of this type:

 


$url = BASE_URL . 'index.php?p=error404';
    header("Location: $url");
    exit;
All your options exhausted.

Please suggest an algorithm of actions. How can I cope with it.

 

htaccess

# Script 2.7 - .htaccess
<IfModule mod_rewrite.c>

# Turn on the engine:
RewriteEngine on

# Set the base to this directory:
RewriteBase /ch02/

# Redirect certain paths to index.php:
RewriteRule ^(about|contact|this|that|search|error404)/?$ index.php?p=$1

</IfModule>

index.php

<?php # Script 2.4 - index.php

/* 
 *  This is the main page.
 *  This page includes the configuration file, 
 *  the templates, and any content-specific modules.
 */

// Require the configuration file before any PHP code:
require('config.inc.php');

// Validate what page to show:
if (isset($_GET['p'])) {
    $p = $_GET['p'];
} elseif (isset($_POST['p'])) { // Forms
    $p = $_POST['p'];
} else {
    $p = NULL;
}

// Determine what page to display:
switch ($p) {

    case 'about':
        $page = 'about.inc.php';
        $page_title = 'About This Site';
        break;
    
    case 'contact':
        $page = 'contact.inc.php';
        $page_title = 'Contact Us';
        break;
    
    case 'search':
        $page = 'search.inc.php';
        $page_title = 'Search Results';
        break;
		
		case 'error404':
        $page = 'error404.inc.php';
        $page_title = 'Error!';
        break;
    
    // Default is to include the main page.
    default:
        $page = 'main.inc.php';
        $page_title = 'Site Home Page';
        break;
        
} // End of main switch.

// Make sure the file exists:
if (!file_exists('./modules/' . $page)) {
    $page = 'main.inc.php';
    $page_title = 'Site Home Page';
}

// Include the header file:
include('header.html');

// Include the content-specific module:
// $page is determined from the above switch.
include('./modules/' . $page);

// Include the footer file to complete the template:
include('footer.html');
?>

  • 0

#2 andrey

andrey

    Newbie

  • Members
  • Pip
  • 10 posts

Posted 1 January 2017 - 8:10 AM

 

Hello everyone!

I try to make a error page based on Larry code. but encountered a problem. There was a Problem with a redirect of this type:

 


$url = BASE_URL . 'index.php?p=error404';
    header("Location: $url");
    exit;
All your options exhausted.

Please suggest an algorithm of actions. How can I cope with it.

 

htaccess

# Script 2.7 - .htaccess
<IfModule mod_rewrite.c>

# Turn on the engine:
RewriteEngine on

# Set the base to this directory:
RewriteBase /ch02/

# Redirect certain paths to index.php:
RewriteRule ^(about|contact|this|that|search|error404)/?$ index.php?p=$1

ErrorDocument 404 http://ch02/index.php?p=error404

</IfModule>

index.php

<?php # Script 2.4 - index.php

/* 
 *  This is the main page.
 *  This page includes the configuration file, 
 *  the templates, and any content-specific modules.
 */

// Require the configuration file before any PHP code:
require('config.inc.php');

// Validate what page to show:
if (isset($_GET['p'])) {
    $p = $_GET['p'];
} elseif (isset($_POST['p'])) { // Forms
    $p = $_POST['p'];
} else {
    $p = NULL;
}

// Determine what page to display:
switch ($p) {

    case 'about':
        $page = 'about.inc.php';
        $page_title = 'About This Site';
        break;
    
    case 'contact':
        $page = 'contact.inc.php';
        $page_title = 'Contact Us';
        break;
    
    case 'search':
        $page = 'search.inc.php';
        $page_title = 'Search Results';
        break;
		
		case 'error404':
        $page = 'error404.inc.php';
        $page_title = 'Error!';
        break;
    
    // Default is to include the main page.
    default:
        $page = 'main.inc.php';
        $page_title = 'Site Home Page';
        break;
        
} // End of main switch.

// Make sure the file exists:
if (!file_exists('./modules/' . $page)) {
    $page = 'main.inc.php';
    $page_title = 'Site Home Page';
}

// Include the header file:
include('header.html');

// Include the content-specific module:
// $page is determined from the above switch.
include('./modules/' . $page);

// Include the footer file to complete the template:
include('footer.html');
?>

 


 


  • 0

#3 Larry

Larry

    Administrator/Writer

  • Administrators
  • 4785 posts
  • LocationState College, PA (USA)

Posted 2 January 2017 - 11:36 AM

I would try

ErrorDocument 404 /error404

You shouldn't need to have the hostname in the ErrorDocument value and you should use the rewrite version just to be clean about it.


  • 0

#4 andrey

andrey

    Newbie

  • Members
  • Pip
  • 10 posts

Posted 2 January 2017 - 1:08 PM

Hello Larry,

on a local server, I could not get the result, error page-404, and did not know what to do next.

Thanks Larry for your book recommendation.


  • 0

#5 Larry

Larry

    Administrator/Writer

  • Administrators
  • 4785 posts
  • LocationState College, PA (USA)

Posted 4 January 2017 - 11:03 AM

I would start by checking the Apache error logs to see the specific problem Apache is having. 


  • 0

#6 andrey

andrey

    Newbie

  • Members
  • Pip
  • 10 posts

Posted 20 March 2017 - 8:49 AM

Hello!
I apologize for missing so long.
artsite1 my site.
Error log :

[Mon Mar 20 15:49:53.618600 2017] [ssl:warn] [pid 2384:tid 232] AH01909: artsite1:443:0 server certificate does NOT include an ID which matches the server name


  • 0

#7 Larry

Larry

    Administrator/Writer

  • Administrators
  • 4785 posts
  • LocationState College, PA (USA)

Posted 20 March 2017 - 12:42 PM

I'm confused: is the page that's giving you a 404 an HTTPS URL?


  • 0

#8 andrey

andrey

    Newbie

  • Members
  • Pip
  • 10 posts

Posted 21 March 2017 - 6:09 AM

I am correcting my mistake.

Where: gtyugt.html - request for an existing page.

artsite1: 127.0.0.1 [21/Mar/2017:13:32:03 +0300] "GET /gtyugt.html HTTP/1.1" 404 1039 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0"

  • 0

#9 Larry

Larry

    Administrator/Writer

  • Administrators
  • 4785 posts
  • LocationState College, PA (USA)

Posted 24 March 2017 - 9:18 AM

Ah, okay, thanks for clarifying. So you're going to a purposefully bad URL but it's not showing the 404 error page, correct? What is the URL at that point and what is being shown instead?


  • 0

#10 andrey

andrey

    Newbie

  • Members
  • Pip
  • 10 posts

Posted 30 March 2017 - 8:02 AM

Hello!

When you invoke an obviously non-existent page with index.php, then the apache produces a standard browser error. From any other page, there is a redirect to index.php.

 


  • 0

#11 Larry

Larry

    Administrator/Writer

  • Administrators
  • 4785 posts
  • LocationState College, PA (USA)

Posted 31 March 2017 - 1:54 PM

Okay, so if you go to http://ch02/blargh, the browser stays on http://ch02/blargh and shows the standard Apache error?

 

But the other .htaccess rules are working?  


  • 0

#12 andrey

andrey

    Newbie

  • Members
  • Pip
  • 10 posts

Posted 2 April 2017 - 1:51 PM

Okay, so if you go to http://ch02/blargh, the browser stays on http://ch02/blargh and shows the standard Apache error?

 

But the other .htaccess rules are working?  

Yes all other .htaccess rules are working.


  • 0

#13 Larry

Larry

    Administrator/Writer

  • Administrators
  • 4785 posts
  • LocationState College, PA (USA)

Posted 3 April 2017 - 2:10 PM

Okay, I tested this myself and here's what worked:

# Script 2.7 - .htaccess
<IfModule mod_rewrite.c>

# Turn on the engine:
RewriteEngine on

# Set the base to this directory:
RewriteBase /ch02/

# Redirect certain paths to index.php:
RewriteRule ^(about|contact|this|that|search|error404)/?$ index.php?p=$1

ErrorDocument 404 /ch02/index.php?p=error404
</IfModule>
Keep in mind that I'm using http://localhost/ch02/about (for example) as my URLs. You'll need to tweak this accordingly. And despite using RewriteBase, I had to add the base to the error URL.

  • 0

#14 andrey

andrey

    Newbie

  • Members
  • Pip
  • 10 posts

Posted 5 April 2017 - 1:41 PM

Hello Larry,

Thank you for attention ! Does not work. What is the reason I will understand. The browser displays a standard error "Object not found!".


  • 0

#15 Larry

Larry

    Administrator/Writer

  • Administrators
  • 4785 posts
  • LocationState College, PA (USA)

Posted 5 April 2017 - 10:20 PM

Could you share the .htaccess file that you're using? What URL are you trying? And does any redirection of that URL occur?


  • 0

#16 andrey

andrey

    Newbie

  • Members
  • Pip
  • 10 posts

Posted 7 April 2017 - 1:24 PM

My file .htaccess

# Script 2.7 - .htaccess
<IfModule mod_rewrite.c>

# Turn on the engine:
RewriteEngine on

# Set the base to this directory:
RewriteBase /artsite1/

# Redirect certain paths to index.php:
RewriteRule ^(about|contact|this|that|search|error404)/?$ index.php?p=$1

ErrorDocument 404 /artsite1/index.php?p=error404

</IfModule>

My URL: http: //artsite1/index.php. And erroneous requests for an existing file, For example http: //artsite1/abrakadabra.html. Yes, redirection proceeds.


  • 0

#17 Larry

Larry

    Administrator/Writer

  • Administrators
  • 4785 posts
  • LocationState College, PA (USA)

Posted 7 April 2017 - 3:00 PM

Okay, to start, if the URL for the site is http://artsite1, then you don't want to use RewriteBase /artsite1/ because the site is effectively in the root. 

 

Similarly, your ErrorDocument ends up being incorrect as it points to http://artsite1/artsite1/index.php


  • 0

#18 andrey

andrey

    Newbie

  • Members
  • Pip
  • 10 posts

Posted 8 April 2017 - 12:33 PM

Okay, to start, if the URL for the site is http://artsite1, then you don't want to use RewriteBase /artsite1/ because the site is effectively in the root. 

 

Similarly, your ErrorDocument ends up being incorrect as it points to http://artsite1/artsite1/index.php

Thank you ! Fixed bug. But there is no redirect. Now I'm studying the settings of my OpenServer. Maybe the reason is there.


  • 0