PHP - Display a 404 Error Without Redirecting to Another Page

display custom 404 error page without redirection in PHP

Please remove your ErrorDocument rule and replace it with following code :

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ error.php [L]

A suggestion by the way: You should put a [L] behind RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} , so that no other rule will override the HTTPS-enforcement.

Your .htaccess will then look like this:

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]

RewriteRule index.html$ index.php [L]
RewriteRule service.html$ service.php [L]
RewriteRule ^([^/]*)\.html$ about.php?pid=$1 [L]
RewriteRule ^cont/([^/]*)\.html$ contact-inner.php?tid=$1 [L]
RewriteRule ^contact/([^/]*)/([^/]*)\.html$ contact-page.php?tid=$1&ona=$2 [L]
RewriteRule ^about/([^/]*)\.html$ about-inner.php?oid=$1 [L]
RewriteRule ^service/([^/]*)/([^/]*)\.html$ service-page.php?oid=$1&ona=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ error.php [L]

<Files 403.shtml>
order allow,deny
allow from all
</Files>

deny from 117.202.102.84
deny from 58.68.25.210

deny from 91.200.13.112
deny from 86.128.130.170
deny from 91.200.13.7
deny from 173.208.206.90

PHP - Display a 404 Error without redirecting to another page

Include the error page in your current page and send a 404 error status code:

<?php
if ($nobody_should_ever_be_here) {
header('HTTP/1.1 404 Not Found'); //This may be put inside err.php instead
$_GET['e'] = 404; //Set the variable for the error code (you cannot have a
// querystring in an include directive).
include 'err.php';
exit; //Do not do any more work in this script.
}
?>

Note that this should be used if the page should never be seen. A better status code for un-authorized access (if the page should be seen by some logged in users) is 403 (Not Authorized).

PHP 404 not found page with no redirect + include page

If you want to stay on the same Url, use absolute path instead of the full url for error handler .

change

ErrorDocument 404 http://blablabla.com/404.php

to

ErrorDocument 404 /404.php

This will rewrite error pages to /404.php

Custom 404 Error Page in PHP

Create a .htaccess file and put the following line in it:

ErrorDocument 404 /errorpageFileName.php 

That will set the page 'errorpageFileName.php to your error 404 page. You can of course change the file name to your likings.

You can read more in-depth about it here:
Custom 404 error issues with Apache



Related Topics



Leave a reply



Submit