Forum Moderators: phranque

Message Too Old, No Replies

404 document not showing correctly in certain directories

         

dpinion

3:54 pm on Jul 14, 2009 (gmt 0)

10+ Year Member



Greetings,
I have .htaccess setup with the following statement:

#Redirect 404 responses to error.php
errordocument 404 http://www.example.com/error.php

I have also done it this way:

#Redirect 404 responses to error.php
errordocument 404 /error.php

The problem is that if I produce a one-off url such as:

www.example.com/bob

Then the 404 (error.php) page displays correctly. However, if I do something like:

www.example.com/dir/bob

Then I get shown the 404 page, however, it is NOT the same page as the other one. Different graphics, my menu is messed up (css seems to be off), and there are links that are not on the correctly working page. I checked and I have no other error.php page in my site, so I am at a loss as to why this is happening.

Anyone have any ideas?

[edited by: jdMorgan at 5:13 pm (utc) on July 14, 2009]
[edit reason] example.com [/edit]

jdMorgan

4:56 pm on Jul 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First of all, this is dangerously wrong:

#Redirect 404 responses to error.php
errordocument 404 http://www.example.com/error.php

As documented in the Apache core documentation, this will return a 302 redirect status, and not a 404-Not Found status. If you like, you can verify this using the Live HTTP Headers add-on for Fireofx/Mozilla or a similar tool. The documentation states that if a full URL is provided, then the result is a redirect response, not a 404 response.

This can make a right mess of your search engine rankings.

With the comment corrected, this other code you used is correct:


# Define custom 404 error page /error.php
ErrorDocument 404 /error.php

Note that this directive does *not* cause or use a redirect. It would be more accurate to call it an internal rewrite, as the content of the error page is simply substituted for the content of the originally-requested resource; The URL does not change, the client is not redirected, and so this is not a redirect of any kind.

Now that may be the cause of your problem -- or part of it: Any relative links or includes on the error page itself will be resolved to absolute URLs by the client according to the URL in its address bar. So images, CSS files, external JavaScript files, etc. will be referenced using incorrect URLs if the links on your error page are page-relative -- The absolute URL will depend on the 'directory level' of the requested (missing) resource's URL, and so will change depending on what URL the client requested.

The solution is fairly simple: On your error page, link to included objects and other pages using server-relative or canonical URLs. That is, use <img src="/images/logo.gif"> or <img src="http://www.example.com/images/logo.gif"> instead of <img src="images/logo.gif">

Check through all directories for additional .htaccess files containing ErrorDocument declarations, and remove any such conflicting or redundant directives; One ErrorDocument directive in your top-level .htaccess file will suffice.

There's little chance that a different error page is being served 'by magic,' so I am assuming that either there is a redundant ErrorDocument directive somewhere, or that the relative links described above are breaking or corrupting your PHP error page and making it look different and wrong. If you clean up the issues described above and still have problems, let us know.

Also, if fixing the relative links 'cures' your PHP error page oddities, it would be good to investigate precisely why it does so (especially the mysteriously-appearing unexpected links on the page) and to make the PHP script more robust so that such secondary effects of relative linking do not occur.

Jim

dpinion

5:21 pm on Jul 14, 2009 (gmt 0)

10+ Year Member



Hey Jim
Thank you for your reply and all the wonderful information. I guess being new to this it is easy to step in a deep hole. :) I did, however, only change to the full URL for the 404 to test it, then switched it back.

I still don't have a clue what is going on though. The page on the lower directories just isn't the same page being served for higher level ones.

For instance, there is a div that creates a "second menu" where items for a particular category chosen on the main menu are shown. In error.php, I completely removed this div and its contents. However, the lower-level error page STILL shows this menu. I have no idea where it is getting it from.

It isn't just that the graphics are showing up, but that they are showing completely different graphics. For example I have a button that was originally orange, but in my 404 page it should be red. It isn't it is still showing orange.

So I am still not sure what is going on. I have checked and there are other htaccess files, which I renamed to htaccess.txt. However this had no effect on the problem.

jdMorgan

6:06 pm on Jul 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Three suggestions:

1) Completely flush your browser cache before and during testing to prevent stale cached pages from showing. You might even want to disable the browser cache if you're sure you'll remember to turn it back on when done.

2) Do a View->Page Source on the "wrong" 404 page, and see if that gives you any clues.

3) Check error.php for "includes" or "requires" of other shared scripts, and make sure that the behaviour of those shared scripts will be correct when called from error.php while processing a 404.

This 'wrong' 404 page has to be coming from somewhere -- Again, 'magic' does not apply in server technology... :)

Jim

dpinion

6:41 pm on Jul 14, 2009 (gmt 0)

10+ Year Member



Thanks Jim,
I went through all suggestions, and its a no go. However, I did see one thing interesting. I compared the source of the correct page to the incorrect one. I noticed one difference:

working:
<head>
<base href="http://www.test.com/index.php/company-information/generalfaq" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

non-working:
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

as you can see the base href element is gone. I looked in the source file and the base href line is in there. So it does appear that I am pulling two different files, just not sure how....

jdMorgan

7:40 pm on Jul 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, the base href is telling you which file you're likely seeing, since it probably specifies itself: /index.php/company-information/generalfaq

So the question becomes, why is that page being served?

Look for other RewriteRules, RedirectMatches, Aliases, or script functions that might get you to that page.

Jim