homepage Welcome to WebmasterWorld Guest from
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 / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

alternative solution to the usual 404 rule

 12:52 pm on Oct 26, 2011 (gmt 0)


I stumbled onto a weird problem.

My htaccess file works with more complex rules like non-www redirect etc. but when I insert a simple rule like below.

ErrorDocument 404 /404/error.html

It just doesn't work ?

the browser send this message:

Not Found
The requested URL /404-link was not found on this server.

Could it be a setting on the clients server that prevents this one simple rule from not working.

And yes I'm doing everything right, and this page does exist on clients server, 404/error.html.

Is there any other rule that does exactly the same as ErrorDocument ie, return 404 status, and show custom error page ?

I even tried removing all the other rules/rewrites, and just kept the ErrorDocument rule, but it also didn't work.



 8:11 pm on Oct 26, 2011 (gmt 0)

Do you mean that you have a top-level directory named 404? Seems an odd way to do it: do all your other error documents each go in their own directories?

If by "client" you mean the people browsing your site, their server settings should be completely irrelevant. The first thing to check is whether you're allowed to use the ErrorDocument line. In general you either can or can't have your own htaccess, and this is a core directive so there's no question of not having the right module.

Some hosts have default names for custom error documents, like "missing.html". If you leave off the ErrorDocument line and put a document with the default name in the default location, does that work?

Edit: The wording "404-link" seems to imply that your server expects something other than a directory name. So experiment #2 is to call it something else.


 8:21 am on Oct 27, 2011 (gmt 0)

404 can be named anything, i just used examples, my htaccess looks like this at the moment:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]

rewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
rewriterule ^/index\.html$ http://www.example.com/ [R=301,L]
rewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.html\ HTTP/
rewriterule index\.html$ http://www.example.com/%1 [R=301,L]

ErrorDocument 404 /404/error.html

But the errordocument just doesn't work. All else is working. very weird.

I even tried removing all other code and just have ErrorDocument rule in, but no luck.

404-link is just an example, so when you enter wrong url eg. www.example.com/wrong-link, then browser send this message:

Not Found
The requested URL /wrong-link was not found on this server

I've tried all below examples also:
ErrorDocument 404 /404.html
ErrorDocument 404 404.html
ErrorDocument 404 /404-error.html
ErrorDocument 404 http://www.example.com/404/error.html
ErrorDocument 404 http://www.example.com/404-error.html

and changed the name of the custom html page accordingly.

But errordocument rule simply doesn't work on this hosting.

Just wondering if it could be a server setting that might prevent ErrorDocument from working ?
Is there another rule that I can try that does the same thing as ErrorDocument ?


 9:55 am on Oct 27, 2011 (gmt 0)

Unless you have special requirements for your 404 which result in a custom html, just go with the standard. In fact, take out all the above, clear your browser cache and test to see if your server is actually returning a standard 404. Start from there.


 11:53 am on Oct 27, 2011 (gmt 0)

I did that yes, the server returns this:

Not Found
The requested URL /wrong-link was not found on this server

I want it to display the custom page I created:

But as I said the ErrorDocument rule doesn't work on this server.

Is there another rule that I can try that does the same thing as ErrorDocument ?


 6:42 pm on Oct 27, 2011 (gmt 0)

I did that yes, the server returns this:

Not Found
The requested URL /wrong-link was not found on this server

Wait. This is the response that you get when you have no rule and no custom error document, using a different browser with a clear cache?

Stop tearing out your hair and have a talk with your host. It should always display something in response to basic errors. Don't try to customize if there's a problem with the default; you risk making things worse.


 6:23 am on Oct 28, 2011 (gmt 0)

That is the standard 404 response.

Put your custom 404 in the root then test again.

Is there something special about your custom 404 that makes it better than the standard response?


 6:40 am on Oct 28, 2011 (gmt 0)

There's serious problems with the mod_rewrite code in that .htaccess file.

As coded, the second rule and condition can never run, because of the leading / after the ^ character. However, the rule isn't needed at all, because the third rule and condition can do this for root index requests anyway. That's what the
* in /(([^/]+/)*) is for. Delete ruleset "2".

The non-www to www redirect doesn't redirect all non-canonical hostnames (such as www with port number). Change the pattern to !^(www\.example\.com)?$ and lose the [NC] flag.

Finally, the rules are in the wrong order. At present a request for example.com/index.html is double redirected:
example.com/index.html => www.example.com/index.html => www.example.com/ and this unwanted redirection chain is a big problem.

Reverse the ruleset order, "3" then "1" and it will fix this. Rules should be ordered from "more specific" to "more geneeral".

For code clarity, add a blank line after every RewriteRule and start each block of code with a plain English
# comment explaining what the next line or two of code actually does.

 6:48 am on Oct 28, 2011 (gmt 0)

ErrorDocument directive must refer to a local file by path only, not domain name.

ErrorDocument 404 http://www.example.com/404/error.html will produce a 302 response, not 404, as explained in the Apache manual.

 8:41 am on Oct 28, 2011 (gmt 0)

That is the standard 404 response.

Heh. I misread his version as literal text and thought he was quoting one of those 500-type errors where it first reports a mistake and then says it couldn't find the document that's supposed to report the mistake :)

Apache actually offers four error-handling approaches
but I have to say that two of 'em I have never seen in my life.

Note that when you specify an ErrorDocument that points to a remote URL (ie. anything with a method such as http in front of it), Apache will send a redirect to the client to tell it where to find the document, even if the document ends up being on the same server. This has several implications,

Again, I misread this as "severe implications", though that may be about right ;)

the most important being that the client will not receive the original error status code, but instead will receive a redirect status code. This in turn can confuse web robots and other clients which try to determine if a URL is valid using the status code. In addition, if you use a remote URL in an ErrorDocument 401, the client will not know to prompt the user for a password since it will not receive the 401 status code. Therefore, if you use an ErrorDocument 401 directive then it must refer to a local document.


 8:52 am on Oct 28, 2011 (gmt 0)

Yes, that's a "severe problem". Sites with this problem often end up serving their 404 error page with 200 OK status after 302 redirecting the user to it. Do not do this.

Serve the 404 status and the human-readable error message at the originally requested URL.


 2:44 pm on Oct 31, 2011 (gmt 0)

RewriteEngine On
ErrorDocument 404 /404/error.html

But something on the server is ignoring this command.

I only have above code in my htaccess and when I enter a wrong url I get this message:

Not Found
The requested URL /foobar was not found on this server.

Could it be that a server directive is set wrong ? ie. AllowOverride Directive, AllowEncodedSlashes Directive or AuthName Directive. Somewhere's a wrong server setting that doesn't allow me to specify a custom 404 page.


 2:46 pm on Oct 31, 2011 (gmt 0)

Note i've taken out anything else (non www and index.php rewrite) from my htaccess and only left this:

RewriteEngine On
ErrorDocument 404 /404/error.html

to simplify life.


 4:14 pm on Oct 31, 2011 (gmt 0)

If you request
example.com/foobarquuxwibble replacing example.com with your hostname, what is the exact error message you see?

 4:33 pm on Oct 31, 2011 (gmt 0)

If you've taken out everything else you can also take out the "RewriteEngine On". Although the 404 document is a special kind of rewrite, it does not use mod_rewrite.

It may be less confusing if you quote the same literal text in your ErrorDocument line and in the response you're getting. (Since you're not giving a domain name, you are not breaking any rules.) That is, if your htaccess says

ErrorDocument 404 /404/error.html

is the response saying

The requested URL /404/error.html was not found on this server.

? Or does the response come with something prepended to the /404 part, like a domain name or a physical path? And do you in fact have a top-level directory called /404 ? (Well, called something, anyway.) Sorry. I'm not trying to be insulting; we just need to start at the basics.


 8:30 am on Nov 1, 2011 (gmt 0)

g1, when I would enter this wrong (non existing) url, example.com/foobarquuxwibble, i get this message in the browser (Fire Fox 7.0.1 by the way):

Not Found
The requested URL /foobarquuxwibble was not found on this server.

lucy, my htaccess have this only, I even took out RewriteEngine On. See below:

ErrorDocument 404 /404/error.html

yes, I have a top level directory called 404, with the file error.html in it.

When I visit my custom page, it will display without a problem.

Note, I've also contacted my host about this now, and they're still busy investigating.


 8:32 am on Nov 1, 2011 (gmt 0)

Note that this is not a caching issue, as I tested this on 3 different pcs from different networks.


 1:59 pm on Nov 1, 2011 (gmt 0)

You ARE getting a proper 404 response. It's just not the one you want.

ErrorDocument 404 /404/error.html

Above indicates you have a folder /404/ which contains document error.html

Try this:

ErrorDocument 404 /404custom.html

Create 404custom.html and place in ROOT directory

Your custom 404 should offer some kind of service to the end user, ie.

"The page requested is not found. It may have recently changed.
Return to example.com."

BOLD above is a homepage link to keep the visitor on your site instead of them hitting back and taking them off site again.

If you have in-site search, you might offer a link to that (local search, not google or bing, though you can do that... though the user will type in the same thing again, and fail again). You might offer link to a directory or in-site menu structure. You might link to a in-site general info page which can suggest direction.


 3:54 pm on Nov 1, 2011 (gmt 0)

I did the above tangor, still exactly the same problem, my custom page DOESN'T come up when a wrong url is entered.

You guys don't understand, the ErrorDocument command doesn't work on this server in this case. That's why it's so weird.

Almost 100% it's some wrong server setting..


 5:53 pm on Nov 1, 2011 (gmt 0)

If you did the above and got the reported result, contact your host and just ask the question: "Are custom 404s allowed?"


 12:01 am on Nov 2, 2011 (gmt 0)

It would be very odd for a host to allow htaccess but not custom error documents. But they might constrain you to pre-determined names in pre-determined locations. Did you ever try the default "missing.html" (check the host's fine print for their preferred names) in the root directory? If you do it this way, you don't need a pointer in htaccess.


 8:25 am on Nov 2, 2011 (gmt 0)

This has been resolved, I contacted my host and they also struggled a bit and couldn't figure it out, then updated apache on the server side and BOOOOM !

sorted ! It's working. Thanks for your help anyway

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
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