homepage Welcome to WebmasterWorld Guest from 54.167.179.48
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Custom 404 Problem
It won't redirect for a bad .php call
dhdweb

10+ Year Member



 
Msg#: 1097 posted 4:15 pm on Oct 19, 2002 (gmt 0)

How do I get the custom 404 page to display if somebody miss-types the URL for a .php page?

All that happens now is the standard can't open php error.

Any way around this?

dhdweb

P.S. I don't have access to the PHP config file!

 

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 1097 posted 4:24 pm on Oct 19, 2002 (gmt 0)

dhdweb,

For custom 404 redirects, the server is looking at the URL, and it does not take into account the query string passed to scripts.

I expect you'll have to handle the error inside PHP. Hopefully, someone here can help you with that.

Jim

jatar_k

WebmasterWorld Administrator jatar_k us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 1097 posted 10:28 pm on Oct 19, 2002 (gmt 0)

jd has it.
dhdweb, is it a query string or the actual page name? A non existant page name will generate a 404 the same way as anything else.

dhdweb

10+ Year Member



 
Msg#: 1097 posted 11:43 pm on Oct 19, 2002 (gmt 0)

Here is the deal:

I have
ErrorDocument 404 /error.htm
in my .htaccess

If I type www.anysite.com/fakepage.htm or .html
I get my error.htm

but if I type www.anysite.com/fakepage.php
I get this:
Fatal error: Unable to open /u/web/*****/fakepage.php in Unknown on line 0

Slade

10+ Year Member



 
Msg#: 1097 posted 12:27 am on Oct 20, 2002 (gmt 0)

you just need to do a redirect (http header, look it up) from PHP if your page isn't found.

dhdweb

10+ Year Member



 
Msg#: 1097 posted 2:19 pm on Oct 20, 2002 (gmt 0)

Is that not what
ErrorDocument 404 /error.htm
does?

dingman

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1097 posted 9:35 pm on Oct 20, 2002 (gmt 0)

The difference is that a 404 error happens when the web server can't find the dicument requested. It has nothing to do with the operation of your script.

Suppose I have a web site [mysite.tld...] on which index.php contains the following


<?php
$foo = $_GET['foo'];

switch ($foo)
{
case "bar":
include("nonexistent.html");
break;

case "baz":
include("existent.html");
}
?>

I visit [mysite.tld...]

Apache, the web server, recieves a request for /index.php?foo=bar. It recognizes the '?' as indicative of a query string, and therefore just looks for a file named 'index.php' in my web root. It finds index.php. Since index.php is an executable program, Apache executes index.php, passing the query string ('foo=bar') on to index.php. Whatever output index.php produces, Apache returns to the user as the result of the request.

Since the query string passed to index.php indicates such behavior, index.php attempts to load "nonexistent.html". If it doesn't exist, the PHP engine prints an error, in HTML, as part of its output.

Apache doesn't know or care what index.php did, it just knows what the output is, and passes that on to the user. As far as Apache is concerned, this was a successfull request.

If you want to treat the absence of that file as a 404 error, you have to write index.php such that instead of generating an error message when it doesn't find the file, it sends a redirect to the 404 error page. Apache doesn't experience a 404, so how Apache is configured to handle 404s matters not at all. Apache found the document the user requested, index.php.

In addition to looking up the header() function, you will probably be interested in file_exists().

<edit> fixed a spelling error so bone-headed even I noticed it </edit>

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 1097 posted 11:55 pm on Oct 20, 2002 (gmt 0)

dingman,

Good write-up!

Jim

Slade

10+ Year Member



 
Msg#: 1097 posted 12:55 am on Oct 21, 2002 (gmt 0)

In my recent coding, I used:


...
case default:
include ('404.php');
...

edit: I really miss [php] from vBulletin.

dhdweb

10+ Year Member



 
Msg#: 1097 posted 7:44 pm on Oct 21, 2002 (gmt 0)

You may have lost me here!

Are you saying that if fake.php is called and does not exist, that the server will then look to index.php for what to do?

P.S. I am not dealing with a query string problem as asked above.

dingman

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1097 posted 8:20 pm on Oct 21, 2002 (gmt 0)

Depends on how you're looking for fake.php. Suppose we have a small web site at [mysite.tld...] In addition to the index.php above, we have a couple other files:

broken.php
works.php
real.php

broken.php contains


<?php
include("fake.php");
?>

and works.php contains


<?php
include("real.php");
?>

If we visit [mysite.tld...] Apache looks for a file called 'fake.php'. It doesn't exist, so Apache doesn't find it. Having failed to find the file, Apache returns a 404 error and displays its 404 error page.

if we visit [mysite.tld...] Apache looks for a file called works.php. It finds it, determines that it is executable, and runs it. Works.php includes real.php, which also exists, and produces some output, which gets added to the output from works.php. Apache sends the output from works.php, which includes the output from real.php, to the browser.

If we visit [mysite.tld...] Apache looks for 'broken.php'. Apache finds broken.php, determines that it is executable, and runs it. Broken.php attempts to include 'fake.php', but fails because the file doesn't exist. The PHP engine therefore adds its error message to the ourput of broken.php. Apache gets this output, and returns it to the user. As far as Apache is concerned, this was a successfull request - you asked for broken.php, and you got it. No different from the works.php example.

Better?

dhdweb

10+ Year Member



 
Msg#: 1097 posted 8:33 pm on Oct 21, 2002 (gmt 0)

If we visit [mysite.tld...] Apache looks for a file called 'fake.php'. It doesn't exist, so Apache doesn't find it. Having failed to find the file, Apache returns a 404 error and displays its 404 error page.

This is the problem: fake.php does not exist! fake was just an example name. So what I am saying is that when a .php file doesn't exist I want my error.htm to disply! Right now it works fine for a bad htm or html request as the Apache 404, but it is not working for a php request.

As it is working now, if some slug types anystupidfile.php, they see:
Fatal error: Unable to open /u/web/*****/anystupidfile.php in Unknown on line 0

and if they type anystupidfile.html or htm they see my error.htm page

dingman

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1097 posted 8:45 pm on Oct 21, 2002 (gmt 0)

Hmm... works for me...

Can you take a look at your Apache config? (If the server isn't Apache, I won't be able to help with config problems, but I'm sure someone here will be.)

Do you know whether PHP is being used as a module or as a CGI program?

dhdweb

10+ Year Member



 
Msg#: 1097 posted 8:54 pm on Oct 21, 2002 (gmt 0)

Yes I am running Apache.

No I don't have access to the config file.

Maybe I should ask this question to my hosting provider!

dhdweb

dingman

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1097 posted 9:05 pm on Oct 21, 2002 (gmt 0)

Asking your hosting provider is probably the best thing to do at this point, yes. All I can say for sure right now is that your host's Apache config is different from either of mine. Not suprising, really. Without seeing the config files, and having never had this particular problem myself, I can't do anything much more useful than making wild guesses.

Sorry I barked up the wrong tree for so long.

dhdweb

10+ Year Member



 
Msg#: 1097 posted 9:37 pm on Oct 21, 2002 (gmt 0)

Thanks for trying anyway! :)

volatilegx

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1097 posted 11:44 pm on Oct 21, 2002 (gmt 0)

I wonder what would happen if you changed

ErrorDocument 404 /error.htm

to

ErrorDocument 404 /error.php

and changed the name of error.htm to error.php?

corpuscle

10+ Year Member



 
Msg#: 1097 posted 1:09 am on Oct 22, 2002 (gmt 0)

I have a suspicion that your web server has had PHP installed as a CGI program instead of an apache module.

I think that apache is calling the CGI PHP interpreter, and passing control to it. The interpreter (and therefore not apache) looks for the missing file, fails, and spits out its error message which apache happily sends to the browser. This is just a theory though :)

Try putting this cheap workaround in your .htaccess file (there are three separate lines here):

RewriteEngine On RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^[0-9a-zA-Z_/.-]+\.php$ /404.html
ErrorDocument 404 /404.html

I haven't tested it though (I have PHP installed as a module, and the 404 error handling in works fine for php and html files) but the above should hopefully translate any non-existent .php URLs to /404.html.

dingman

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1097 posted 2:40 am on Oct 22, 2002 (gmt 0)

Corpuscule - that was my 'wild guess'. I've never used a system where PHP was configured as a CGI interpreter instead of a module, but it would make sense to me.

volatilegx - tried it both ways on my development server, just in case, and at least with the module configuration it makes no difference. I can't think of any reason why it would, either, though that's hardly conclusive evidence.

Splatt

10+ Year Member



 
Msg#: 1097 posted 11:00 am on Oct 22, 2002 (gmt 0)
Try this :

ErrorDocument 404 http://www.yourdomain.com/error.htm

corpuscle

10+ Year Member



 
Msg#: 1097 posted 11:36 am on Oct 22, 2002 (gmt 0)

dingman,

Did you try adding those three lines to your .htaccess file? The Rewrite rule should fix your problem with any luck...

dingman

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1097 posted 1:20 pm on Oct 22, 2002 (gmt 0)

I'm not having the problem :) I tried doing what dhdweb was doing on my development box, and it worked without a hitch for me. That said, if we are right that her host has a cgi php setup rather than an Apache module, I think your rewrite rule ought to do the trick.

JuniorHarris

10+ Year Member



 
Msg#: 1097 posted 3:20 pm on Oct 22, 2002 (gmt 0)

I don't use php but had a similar problem with asp.

Any request for non-existent HTML files would call the custom 404, but requests for non-existent ASP files would not! I would have expected the missing ASP script to generate a 404, but it actually generates a server error 500.

I assume the error code difference is a result of how the web server handles the requests for the different file types. HTML is basically a "hand-off" whilst the script must first be read and processed by the server.

I was able to get around my problem by simply including a custom 500 error page in addition to the 404. Not sure if this will help your problem, but it may be food for thought.

dhdweb

10+ Year Member



 
Msg#: 1097 posted 10:21 pm on Oct 22, 2002 (gmt 0)

Will phpinfo() tell me how it is installed?

I wonder what would happen if you changed
ErrorDocument 404 /error.htm

to

ErrorDocument 404 /error.php

and changed the name of error.htm to error.php?

Already tried that ...... lol

That said, if we are right that her host has a cgi php setup rather than an Apache module....

Make that him :)

dhdweb

dhdweb

10+ Year Member



 
Msg#: 1097 posted 10:27 pm on Oct 22, 2002 (gmt 0)

Ok, I just found this in phpinfo()

SCRIPT_FILENAME /u/httpd/cgi-bin/php4.cgi

Does this tell us what we need to know?

dhdweb

dingman

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1097 posted 12:10 am on Oct 23, 2002 (gmt 0)

I think it does. Looks like a CGI configuration to me. Try that rewrite rule? I don't know much about mod_rewrite myself, but it looked like a sound theory.

<added>Just noticed your gender specification. Sorry, I thought I remembered some mention that you were female in another thread. I wonder who I'm using a male pronoun about when I shouldn't?</added>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved