How do I set a custom 404 for a php herokuapp? - php

I see all these threads for setting custom 404s and other error codes for a RoR herokuapp, but I see nothing for plain HTML apps (in this case, just using a one-line index.php to serve the home.html file) How would I get herokuapp to stop showing the default "This file isn't on the server" to my own? I tried changing the error page in the settings, then realized that's for when the app doesn't even load, so that won't work. (Basically I want this to show instead of this) Any suggestions?

This really is an Apache-specific question. Create a .htaccess with ErrorDocument 404 404.html. http://httpd.apache.org/docs/current/mod/core.html#errordocument

Related

How to show custom 404 error page for the subfolder in php?

I am developing a project in which I want to show my custom error page for 404 error. The codes that I have used are working but not totally. So at first, I am showing the structure for my project then the problems I have.
My site name is www.example.com in which I have 4 files and a folder, files are index.php, about.php, error.php, .htaccess and folder name is admin.
In the admin folder I have lots of pages that I am loading with one single page in index.php. the URLs to access any page I am doing it with www.example.com/admin/index.php?page=login like this, so when page is login it shows me the login page.
Up to this everything is good, now I have used a code in .htaccess
ErrorDocument 404 /error.php
So after using it when I am trying to access a wrong URL such as www.example.com/abt.php it shows me the code written in error.php which is quite natural for my code.
So when I am trying to access again two wrong URLs like www.example.com/adminindex.php?page=login and www.example.com/admin/inx.php?page=login it is not showing me the code written in error.php rather it shows me just a message File Not Found.
I want that these two types of wrong URLs it should show me the code written in error.php.
Interestingly what I want is easily happens in localhost but not on in server.
I cannot understand why it is working perfectly fine in localhost but not on the server.
So how can I do that please help me to complete it?
I don't know that whether it can work for you or not? But you can give it a try i think it can resolve your issue which is quite similar to the previously asked question on stack overflow htaccess 404 page redirect not working for sub directory path
brilliantly answered by #anubhava. I hope it can help you to solve the issue. Also if the above does not help you out then you can also visit this 2 links SOLVED "File not found." instead of custom 404 file with php-fpm enabled & SOLVED "File not found" appears instead of 404.shtml (easy htaccess solution)
In fact there is no good method for that without using mod_rewrite.
The only solution would be to put another .htaccess to your /admin subdirectory (with a valid path for your error.php)
in httpd.conf file (the is in AppServ\Apache24\conf directory)
put this
ErrorDocument 404 /error.php
if not be ok try save the file as html

Apache is not returning 404 error for non existing URL

here is what I want
if someone entered a link that does not exist ,then it should throw 404 Error page of webhost,But currently it is returning 200.
www.example.com/wrong_page.php --> this should throw an error 404 but
is is throwing 200 right now.I SEE SAME PAGE AS
www.example.com/index.php
What might have gone wrong ? is there any configuration that is needed to be done in the htaccess.php ? please help
Most likely you have some already existing rewriting rule in effect, that is all.
Something that redirects all requests to /index.php which actually is a quite common thing in web applications. Such index script acts as a router, processing all requests. This serves as a central entry point and simplifies to define the structure of the implemented engine.
So you should look for already existing internal rewriting rules inside the http servers host configuration or in some dynamic configuration file (".htaccess").
let php send the header 404: (in wrong.php)
<?php
header("HTTP/1.0 404 Not Found");
?>
re-reading your question: is wrong_page.php your error page, or just your example of the non exsiting page?
If so: do you have a custom error page on you site? Then place this headerline in that file
edit: (question was changed)
So the index.php is still loading.
Is no 404 template for seen?
maybe there is some page that actually exists at that location. Otherwise that would never be the case

How to capture missing URLs (Error 404) using 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.

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