How to capture missing URLs (Error 404) using PHP? - php

In my webserver, I have created a .htaccess file with the following lines to redirect the wrong/missing URLs (Error 404), to the php file not_found.php. It works fine.
ErrorDocument 404 http://myserver.edu.in/not_found.php
Can I able to capture list of wrong/missing URLs into a text file (or) display the wrong/missing URL in the error page not_found.php itself?
I tried the PHP variable $_SERVER['REQUEST_URI'] in the not_found.php file to capture the wrong/missing URLs. Now I have two issues.
Since I have used the full URL of the error page in .htaccess, it fails. If I change it to ErrorDocument 404 /not_found.php. It works fine only inside the same directory.
In the not_found.php file, I have used include_once function to include header, footer, images, and scripts. So, If I change the .htaccess to ErrorDocument 404 /not_found.php, the error page not_found.php does not display properly in the different directory.
For example, http://myserver.edu.in/xxx/trial.php
In this case, I used full URL for images and scripts. But, not able to use full URL for PHP files using include_once function.
Can anybody suggest some better ideas to solve this issue?

With the ErrorDocument directive, Apache also sets some special environment variables, for example:
REDIRECT_URL
REDIRECT_QUERY_STRING
These should be available if (as you already noticed) the error document does not have a path starting with http. Thus, you can store the not found requests in your database or some text file.
However, $_SERVER["REQUEST_URI"] is available as well, I am using it in a similar case to redirect requests to another domain, if the file is not on that specific server.

You need to rewrite the missing urls, or even better, you could use PHP to include the not_found.php file. The problem with redirecting that that search engines will think that the directory /not_found.php cannot be found and will think that the redirected url is just being redirected.
Example:
<?php
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found", true, 404);
include("notFound.php");
?>
EDIT: If you are using this in the event of a file not existing on the actual server you will need to rewrite.

Related

How to set https error status to 404 using PHP such that it can be handled by .htaccess file?

I am using PHP for my project and I have .htaccess file which has code below;
ErrorDoccument 404 /error-handler.php
This code is used to send or use error-handler.php file for every 404 error. Everything till here is working. But I want a file named call-404.php which when opened from browser should show 404 error and it must be handled by .htaccess file above. I tried using;
http_response_code(404);
also,
header("HTTP/1.1 404 Not Found");
All the above code does says HTTP ERROR FILE NOT FOUND but the 404 at this condition does not does as per .htaccess file. It just displays browser defined 404 error page. What is the solution for it?
And This question is not simply 404 redirection using .htaccess or simply use of http_response_code(404);. This question shows and wants solution of inability of .htaccess redirection on response to http_response_code(404).
The whole point of ErrorDoccument 404 /error-handler.php is that if NO resource is found (be it image, php script or whatever) then apache will use that directive and head to that 404 page. If however you have reached one of your own scripts, then a resource was found and therefore that .htaccess rule will not be triggered.
But you're almost there. Just header and then call the script!
<?php
header("HTTP/1.1 404 Not Found");
echo file_get_contents('/path/to/your/404page.php');
I presume that the 404 page has nothing dynamic and is just plain HTML?

Apache mod_rewrite returning file as a 404 instead of 200

I want to use a php file to dynamically generate a sitemap.xml, the php file lives outside of the document root.
So in the vhost for the front-end site I tried adding a mod_rewrite RewriteRule:
RewriteRule "/sitemap.xml" "/var/www/elsewhere.../sitemap.php"
This works, as in on the front end the generated sitemap is visible but the file is returned with a 404 status code meaning google will completely ignore it. I can't figure out why it's being returned as a 404? Any help appreciated.
I also tried using Alias instead of RewriteRule, and it worked the same, returned the desired output but as a 404.

ErrorDocument redirection from any directory to a file with relative links - most compact way

I have a php file that works like a custom message displayer. I pass some variables with GET and it displays a message based on that. It also works as a 404 error page. The problem is; it has relative references, which are highly desirable because of portability, but they don't seem to work, after my ErrorDocument redirection to that file.
file's location: /a/b/c/custom_message_displayer.php
When httpd.conf is set to:
ErrorDocument 404 /a/b/c/custom_message_displayer.php?e=404
and the error comes from a subdirectory eg. /a/blah, the active directory is in this case /a/, not the /a/b/c/ to where ErrorDocument is redirecting, and that results in mislinking in the file (relative css, js, include() paths).
I made a workaround that uses an additional file. But i'm concerned about this, and wonder if there's some more simple way of doing this. Perhaps an httpd.config-based redirection that also changes the active directory?
The workaround:
ErrorDocument 404 /additional_file.php → this file redirects using:
header('Location: /a/b/c/custom_message_displayer.php') also preserving/forwarding the GET query.
The file then gets executed in it's directory, and all relative links work.
Is there a simplier way of doing the above? Thanks for any bit of advice.

How to create 404 error page in php

I am using php. I have been to create a custom error page using .htaccess file , the problem is i was not able to upload it on the server using ftp, even after trying in so many ways, so Is there any solution for this, so that I can create a custom error page without the ".htaccess" file.
What do you mean, "create a custom error page"? If you want to provide one to replace the default Apache 404 page, you will need to tell Apache to use YOUR file instead of serving up the default response. The standard method for that is to use a .htaccess, and have
ErrorDocument 404 /uri/leading/to/your/script.php
relevant docs here.
Simply uploading a random file will not magically make Apache use it when a 404 occurs - that's where the ErrorDocument directive comes in. As well, .htaccess does not "create" an error page. It simply tells Apache where to look when one does occur.

I have the URL www.something.com/HereWeGo, I want to get HereWeGo without the 404 error

Consider the URL www.something.com/HereWeGo. When someone enters the url, the site shows a 404 error since the directory does not exists. I can parse the URL to get the query and HereWeGo, but cannot stop it from going to a 404 error.
How can I achieve it by PHP, if it is possible to avoid .htaccess now
Thanks
Jean
You will need to rewrite your URLs so /something directs to index.php?q=/something, the thing is your web server usually throws a 404 by default if the file / directory cannot be found and there are no RewriteRules associated with it, even if you can parse the URL from the error page and find the information you need.
Is there a valid reason why you can't use .htaccess? I would strongly suggest you use it if you can. Not least because at the moment a 404 will mean Google will not index your page.
This article will help you.
You just can't avoid "touching" .htaccess, at least for rewriting all requests to single file ( or defining default 404 file ). You are using Apache, not PHP for the server, right?
Take Kohana 3 Framework for example ( and most other frameworks ), all requests are rewritten to index.php file, which then handles the routes for the request.
RewriteRule .* index.php [L]
Code below will handle everything that responds with a 404 and redirect it.
ErrorDocument 404 /somefile.php
In the somefile.php you can then check if referer is 'local' ( $_SERVER['HTTP_REFERER'] ) and which query string it was about, responding with whatever you want.
You could use mod_rewrite for that specific folder, or you could use your own custom 404 page (read this article).
this is something that you need to configure on the webserver, in a .htaccess file. Take a look at the syntax for the redirect or rewrite. It isn't possible to do this in PHP alone as it is the webserver which processes the URL request.
IIRC, setting up custom 404 (and other web server errors) are part of your webserver environment.
You can't avoid a .htaccess redirect unless you change your apache (or other webservers) configuration to redirect your 404 errors to your own *.php file instead of the default error message.
You have to setup a php file, such as 404.php as your 404 error handler file. After doing this have code in 404.php that looks something like this:
if (stristr($_SERVER["REQUEST URI"],"HereWeGo")
{
//add handling code here
}
On your destination page simply call--
header('HTTP/1.0 200 OK');
--to override the default 404 status code.

Categories