catching all invalid URLs - php

I recently upgraded a site and almost all URLs have changed. I have redirected all of them (or so I hope) but it may be possible that some of them have slipped by me. Is there a way to somehow catch all invalid URLs and send the user to a certain page and somehow know what URL the person came from so I could log this, and fix those? I'm thinking I could use .htaccess somehow but am not sure how. I am using PHP Thanks so much!

You could use a custom ErrorDocument handler written in PHP to catch URLs having "slipped by":
# .htaccess file
ErrorDocument 404 /not-found.php
And in not-found.php:
switch($_SERVER['REDIRECT_URL']) {
case '/really_old_page.php':
header('HTTP/1.1 301 Moved Permanently');
header('Location: /new-url/...');
/* As suggested in the comment, exit here.
Additional output might not be handled well and
provokes undefined behavior. */
exit;
default:
header('HTTP/1.1 404 Not Found');
die('404 - Not Found');
}

in .htaccess in the web root
ErrorDocument 404 /yourFile.php

Easiest would be to add a custom 404 redirect in htaccess.
Just add something like this:
ErrorDocument 404 /errors/404.php
to a .htaccess (create one if needed) in your application root. And all request to none extistent pages will be redircted to your own custom 404 page.

Related

How can I redirect to 404.php when the page doesn't really exist?

I have this 404 redirection in my .htaccess file :
ErrorDocument 404 /404.php
It works fine when I try to access a page that doesn't really exist on my server.
The problem is that when I return my own HTTP 404 status in a page that really exists on the server, it doesn't redirect me to the 404.php page, but the browser displays a standard 404 error.
I return my own HTTP 404 status when I have a wrong url parameter for example.
Can you help me to define the rule in the .htaccess file, please?
in your .htaccess file you should have this rule
RewriteEngine On
ErrorDocument 404 /404.php
and if you want to simulate this error in anyplace other than typing it manually in the header you should use http_response_code
PHP code
<?php
http_response_code(404);
include('my_404.php'); // provide your own HTML for the error page
die();

Redirect wrong urls to 404 page

How to redirect wrong urls to 404 page?
For example, subdomain.domain.com is fine, but if users enter subdomain.domain.com/asadasd/ada or anything like that, index page is still shown. How to make these mistyped urls show 404 page?
Im using php, mysql, cpanel ect ect.
Yes, I am already using htaccess with ErrorDocument 404 /error.php but no luck here.
you can use '.htaccess'
put this line in .htaccess ErrorDocument 404 /error.php and create error.php file in your root folder.
You will handle the redirection with .htaccess errordocument 404 /404.php
Please follow the complete process in this link
It will help you a lot.
error redirection link tutorials

Custom 404 Error Page in PHP

I am a novice in PHP, I hope you can help me with this problem.
How can I create a custom 404 error page in PHP without going anywhere or redirect it to other page.
Ex. mysite.com/no-where
The page no-where does not exists. I want to display a 404 error message with design on that URL.
How can I get the current URL? I got an error on this. Coz I want to check the header if its 404.
$url = $_SERVER['REQUEST_URI'];
$array = get_headers($url);
$string = $array[0];
if(strpos($string,"200")) {
echo 'url exists';
} else {
echo 'url does not exist';
}
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
It's really important to have a correct 404 error page, because:
It helps your site / webapp visitors to guide for the correct place in your site
Much better SEO
It's just looks better
In additional the the answers here, I would suggest you to have different messages for different error types. For example, define in your .htacess file an error page for different failure types:
ErrorDocument 500 /500.html
ErrorDocument 401 /401.html
ErrorDocument 403 /403.html
More information about custom 404 error pages.
If you are using apache as a web server you can create a file named .htaccess in the root directory of your website. This can contain various rules, including error handling, as follows:
ErrorDocument 404 (Your site error 404 page URL)
For example:
ErrorDocument 404 http://example.domain/custom-404-page.html
add this line in .htaccess
ErrorDocument 404 "Page not found."
Use a 404 header and then require your 404 template / page.
header('HTTP/1.0 404 Not Found');
include '404.php';
exit;
That or you can configure Apache to serve up a custom page.
If you are using apache as a web server and cpanel, just click menu error page, choose 404 error not found and edit the editor with your custom html page and save. You will get automatic file 404.shtml in your folder.
If you are using vps openlitespeed and webadmin panel, there is error not found and fill with your custom 404 html page.
If your panel is cyberpanel at openlitespeed, just create your custom 404 html page (ex:404.html), then edit your vHost Conf and fill with line:
errorpage 404 {
url /404.html
}
And save it

PHP - Header 404 not triggering htaccess ErrorDocument

This isn't my exact PHP as it's rather complicated, but it's the same general idea.
if($_GET['page'] == ".htaccess"){
header("HTTP/1.0 404 Not Found");
}
When this happens, Apache doesn't load the 404 page set in the .htaccess file. I know the 404 works because when I go to a non-existent page, I get the specified 404 page.
Is there any way I can get the specified 404 page to load without manually dumping the contents of the 404 page file?
Since Apache already determined that the file actually exists, it wont look for 404 again.
One workaround could be actually sending a Location-header to a actual non-existant page and let Apache handle it. Another could be fetching the 404 page contents through PHP and outputting it together with a Status: 404-header
This may Work
if (strstr($_SERVER['REQUEST_URI'],'.htaccess')){
header('HTTP/1.0 404 Not Found');
exit();
}

Force 404 with PHP and .htaccess not working

I am attempting to throw my own 404 in PHP depending on certain GET vars. But the following is not working. I can confirm that the pge header is coming back with a '404 status code' though. .htaccess just doesn't seem to be redirecting correctly. Am I missing something?
PHP Code:
if(!$_GET['page']){
header('HTTP/1.0 404 Not Found');
}
.htaccess Code:
ErrorDocument 404 /404.html
Many thanks!
As far as Apache's concerned, it's done its job as it has properly found the page/script that the user's request called for. The fact that the script is outputting a 404 header is irrelevant to Apache, since its job was completed properly.
You'd need to have your PHP script output the 404 header, and include the page the Apache 404 handler points at it:
if (!$_GET['page']) {
header('HTTP/1.0 404 Not Found');
include('404.html');
exit();
}
Don't do a redirect to the 404 page, as that'd just turn the hit into a normal GET request, with a 200 OK result.
You cannot make the error page start with a number on a XAMPP test server I know...
It may be true for windows ... period.
that one eluded me for hours!
Try renaming your error page to:
error404.html
then change your .htaccess Error section for your 404 to:
ErrorDocument 404 /error404.html
And don't forget to start it with the forward slash. That will fix it.

Categories