3

According to apache documentation, the final parameter of ErrorDocument can be specified as a path, URL or text to any error code.

What I have done so far to eliminate any calls to the I/O file system is to use text as my final value. For example, I use the following in my httpd.conf file:

ErrorDocument 404 "Error: Not Found"

And of course when I restart apache and a user requests a file not being found, they only see "Error: Not found" on the screen.

All is well until I trigger the error in Google's page-speed insights.

Here's the URL to the test:

https://developers.google.com/speed/pagespeed/insights/?url=http%3A%2F%2Fthenew.clubcatcher.com%2Fpictures%2Fnotexist

Pagespeed tells me to configure the viewport and to use legible font sizes.

I personally think its an overkill considering that I made the size of the error pages so small to prevent bots from sucking up my bandwidth, and now it seems the solution is to add a 1000% byte increase per error triggered just to make google happy. (That's right, I'd have to go from 50 or so bytes to over 500).

I could rant more, but what I'm considering to make everyone happy is to format my error pages to contain HTML but without the need of accessing the file system. For example, I want to include something like this in my httpd.conf:

ErrorDocument 404 '<html><head><meta name="viewport" content="width:device-width"></head><body>Not found</body></html>'

Such HTML would make the page more compatible with mobile users, but the question is, would apache accept html as text? and if so, how do I rearrange it in the best form so that apache outputs it as raw html so that the browser converts the data correctly for the client? I ask that because my value has quotes in it and I don't want apache thrown off.

1 Answer 1

1

Can html go in the ErrorDocument value internally in httpd.conf?

Yes.

HTML is just text and Apache appears to send this with a Content-Type: text/html header by default anyway. So, when this "text" hits the browser the browser renders it as HTML, even when sending a simple string of text.

...would apache accept html as text?

As stated above, HTML is text. It's up to the browser to do something with it (providing it knows what to do with it).

how do I rearrange it in the best form so that apache outputs it as raw html

What you already have looks good to go.

because my value has quotes in it and I don't want apache thrown off.

The argument passed to the ErrorDocument directive needs to be delimited by quotes (either double or single) for it to be interpreted as text.

If your text contains the same quotes you are using to delimit your text (eg. using double quotes in a string surrounded by double quotes) then these must be backslash escaped. eg. "<meta name=\"viewport\" content...". However, don't escape quotes when they don't need to be (eg. double quotes in a single quoted string), otherwise the backslash is simply seen as a literal backslash.

Using single quotes to delimit HTML would seem to be the best option for this reason. (Assuming double quotes are used for attribute values.)

If Apache does get "thrown off" you'll likely get a 500 error - so you'll know very quickly that something is off!

Just a thought, to make it readable on the small screen you could just use an h1 tag, rather than a full/compliant document? (Does Google Pagespeed recognise this?)

ErrorDocument 404 "<h1>Error: Not Found</h1>"

eliminate any calls to the I/O file system

However, I do kinda wonder whether this really makes any difference?!

A verbose 404 with helpful information and links for the user is an opportunity to keep the user on your site and stop them from bouncing. This might be more important than the file I/Os and extra bandwidth that a bot might consume. (?)

4
  • I'm asking this so I can craft the perfect default error message for for ALL sites and for all devices attached to my server including mine. I already have pretty error messages for my main site. And yes it can make a huge difference, especially when bots constantly try to access non-existing documents like wp-admin.php. Commented Apr 21, 2016 at 3:08
  • "the perfect default error message for for ALL sites and for all devices" - Not sure that I follow. By "default", you are implying this can be overridden (at runtime) in some way? What do you mean by "perfect"? There's really no such thing as the "perfect" one-size-fits-all error message? The "perfect" (for your user) error message is customised per site and even per request. The perfect for a bot is just a status code (empty response).
    – MrWhite
    Commented Apr 21, 2016 at 8:13
  • In your question the only concern is for readability on mobile devices (or rather, the error that Pagespeed reports for mobile devices). But if the only text you are returning is "Error: Not Found" - is that really a concern? The user on a mobile device can still read it quite easily if they want to.
    – MrWhite
    Commented Apr 21, 2016 at 8:18
  • It depends on mobile settings I think. Some users could set their text to be "far" so that they see more of the screen at once on a tiny device where as each letter is almost unrecognizable. Commented Apr 22, 2016 at 3:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.