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?
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.
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.
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.
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.