Track links that causes 404 - php

I am trying to track all those link that causes 404.i tried $_SERVER['HTTP_REFERER'] on 404 page but it doesn't work for me .
NOTE: i don't wanna use google analytic API. Is there any other possible way

Create a .htaccess file that will redirect to a 404 page on 404 error.
Add this code to your .htaccess file: ErrorDocument 404 /404.php and in the root of your code create a file called 404.php. In that 404.php file you can do all the handling of the error.
$_SERVER['REQUEST_URI'] should have the URL that was requested in the browser.
Also make sure that you are loading the 404 page and not redirecting to the 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();

How correctly redirect to error page? php

How I can redirect to my error page?
My directory structure:
Inside .htaccess file:
ErrorDocument 404 /Index.php?controller=Main&action=error
I don't know how correctly to tell, but it redirects me to ../ back one level.., in xampp ../ will be Index file...And show:
I want on 404 error redirect to Index.
I try in .htaccess file add this line:
ErrorDocument 404 Index.php?controller=Main&action=error
but without / flash its show text like this:
Enter an absolute URI, like ErrorDocument 404 /Index.php?controller=Main&action=error
See https://httpd.apache.org/docs/2.4/mod/core.html#errordocument

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

Problem with apache mod_rewrite and 404 pages

I have a custom 404 error page, and a mod_rewrite rule. If I access a page that does not exist, I get my 404 error page. My problem is that if I issue a 404 header from my php page, it does not open my 404 page, instead, I get this:
Not Found
The requested URL /index.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying
to use an ErrorDocument to handle the request.
This is my .htaccess:
RewriteEngine on
ErrorDocument 404 /errors/404.php
RewriteRule ^[A-Za-z0-9]{8}/$ /index.php
This is my redirect from /index.php that will 404 only if the key does not exist. The $key is obtained from parsing the URL (e.g. http://localhost/aKeYCoDE/):
<?php
if (!key_exists($key)){
header('HTTP/1.0 404 Not Found');
exit;
}
?>
I am expecting it to redirect to my 404 page.
UPDATE:
It is definitely something about the fact that I am calling 404 from a page that was rewritten (/index.php). If I create a dummy page: /redirect.php, and then do nothing but issue the 404 from there, I get my custom 404 page. But, if I write a mod_rewrite rule for it, and try to access it that way, I get the default 404 error page.
I found the answer - the problem was my assumption that a header 404 from php would redirect to a 404 page. That was wrong. The Apache server can issue 404 for pages that do not exist, but for pages that do exist, i.e., are being served by the php page, the 404 response goes to the browser.
This thread was of a similar issue:
http://www.webmasterworld.com/forum88/10955.htm
To make php do what I want it to do (issue the 404 when key does not exist), I need to include the 404 page from php:
<?php
if (!key_exists($key)){
include($_SERVER["DOCUMENT_ROOT"]."/errors/404.php");
exit;
}
?>

htaccess 404 include page

I know I can redirect to a 404 page with htaccess, but can I just include the custom 404 page I have into the URL that is causing the 404? For example, someone types in blahwat.php, which is not on the server, can I keep them on that page but instead of the apache 404 can I have it include my custom 404 page?
Using ErrorDocument does not redirect, so I don't know why you're bringing redirection up.
In any case, if there was a page at the URL then you wouldn't be getting a 404 in the first place.

Categories