2

In my .htaccess I have

ErrorDocument 404 http://foo.com/404.html

It works, in the sense that I recover from any file not found error and show the 404 page instead, but the way it works is to redirect the erroneous URI using a 301 to my 404.html page.

This is bad because search engines and crawlers will not end up seeing a 404 code, rather a 301 code and will not purge deleted pages from their indices.

Can I make Apache serve up the contents of this 404 page upon file not found errors, yet still return a 404 code instead of the 301?

3

You should try to use absolute or relative path and not an URL

My apache2.conf :

ErrorDocument 403 /error/403.html
ErrorDocument 404 /error/404.html
ErrorDocument 500 /error/500.html
4

Don't point at an external url

Apache's docs say:

The syntax is,

ErrorDocument <3-digit-code> <action>

where the action can be,

  • Text to be displayed. Wrap the text with quotes (").
  • An external URL to redirect to.
  • A local URL to redirect to.

Pointing at an external url will cause an actual redirect, whereas using a local url is internal.

So, to render the contents of a file simply point at it e.g.:

ErrorDocument 404 /404.html

Use the right status code for the job

This is bad because search engines and crawlers will not end up seeing a 404 code, rather a 301 code and will not purge deleted pages from their indices.

If the page used to exist and now does not, the correct response code is 410 Gone. 410 is the code to say "hey, that doesn't exist" whereas a 404 will only be taken to mean that after repeated attempts to access the now-none-existent page.

  • True, 410 is correct for that case, but it was just an example. – coneybeare Nov 24 '14 at 15:13
0

Comment to Maxence’s and AD7six’s answers (I don’t have eneough reputation on serverfaul, and stack exchange reputations doesn’t count apparently):

I just solved the problem this way, but then the included pictures and styles were missing since the URL is still the URL that the userr was trying to call.

The solution is to add a base tag in the head of the 404 dowument like

ErrorDocument 404

or to use absolute URLs with or without protocol/domain name.

  • You need 200 reputation on one site to be able to comment on all sites. – kasperd Feb 8 '17 at 22:36
  • I think using absolute HTTP URLs was actually what caused the initial issue - did you mean absolute paths? – iwaseatenbyagrue Mar 8 '17 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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