Forum Moderators: phranque
#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]
#Redirect 404 responses to error.php
errordocument 404 http://www.example.com/error.php
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
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
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.
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
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....
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