htaccess ErrorDocument Examples

Anything not fitting into other categories

htaccess ErrorDocument Examples

Postby produke » 26 Sep 2006 20:45

ErrorDocument Usage and Info

STATUS CODE LIST

401 Unauthorized
The request requires user authentication. The response MUST include a WWW-Authenticate header field (section 14.47) containing a challenge applicable to the requested resource. The client MAY repeat the request with a suitable Authorization header field (section 14.8). If the request already included Authorization credentials, then the 401 response indicates that authorization has been refused for those credentials. If the 401 response contains the same challenge as the prior response, and the user agent has already attempted authentication at least once, then the user SHOULD be presented the entity that was given in the response, since that entity might include relevant diagnostic information. HTTP access authentication is explained in "HTTP Authentication: Basic and Digest Access Authentication" [43].

403 Forbidden
The server understood the request, but is refusing to fulfill it. Authorization will not help and the request SHOULD NOT be repeated. If the request method was not HEAD and the server wishes to make public why the request has not been fulfilled, it SHOULD describe the reason for the refusal in the entity. If the server does not wish to make this information available to the client, the status code 404 (Not Found) can be used instead.

404 Not Found
The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable.



In the event of a problem or error, Apache can be configured to do one of four things,
  1. output a simple hardcoded error message
  2. output a customized message
  3. redirect to a local URL-path to handle the problem/error
  4. redirect to an external URL to handle the problem/error

The first option is the default, while options 2-4 are configured using the ErrorDocument directive, which is followed by the HTTP response code and a message or URL.

Messages in this context begin with a single double-quote character ("), which does not form part of the message itself. Apache will sometimes offer additional information regarding the problem/error.

URLs can begin with a slash (/) for local URLs, or be a full URL which the client can resolve.

Examples:
Code: Select all
ErrorDocument 500 http://foo.example.com/cgi-bin/tester
ErrorDocument 404 /cgi-bin/bad_urls.pl
ErrorDocument 401 /subscription_info.html
ErrorDocument 403 "Sorry can't allow you access today


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, 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.

Otherwise you could see an error message like
Code: Select all
[notice] cannot use a full URL in a 401 ErrorDocument directive --- ignoring!

or
Code: Select all
"cannot use a full URL in a 401 ErrorDocument directive"


Microsoft Internet Explorer (MSIE) will by default ignore server-generated error messages when they are "too small" and substitute its own "friendly" error messages. The size threshold varies depending on the type of error, but in general, if you make your error document greater than 512 bytes, then MSIE will show the server-generated error rather than masking it. More information is available in Microsoft Knowledgebase article Q294807.


Other Examples:

Specify a local file
Code: Select all
ErrorDocument 404 /cgi-bin/error.php


Specify a remote file
Code: Select all
ErrorDocument 404 http://www.askapache.com/


Specify an https file
Code: Select all
ErrorDocument 404 https://secure.askapache.com/


This goes in your / directory (webroot)
Code: Select all
ErrorDocument 404 /cgi-bin/error.php
ErrorDocument 400 /cgi-bin/error.php
ErrorDocument 401 /cgi-bin/error.php
ErrorDocument 403 /cgi-bin/error.php
ErrorDocument 405 /cgi-bin/error.php
ErrorDocument 406 /cgi-bin/error.php
ErrorDocument 409 /cgi-bin/error.php
ErrorDocument 413 /cgi-bin/error.php
ErrorDocument 414 /cgi-bin/error.php
ErrorDocument 500 /cgi-bin/error.php
ErrorDocument 501 /cgi-bin/error.php



This is another option that will also redirect failing requests.

Code: Select all
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /404.php [L]
User avatar
produke
 
Posts: 242
Joined: 25 Sep 2006 04:48

Postby produke » 28 Feb 2007 16:42

Best HTTP Status Code Info

Sample:
  • Code - Message Description
  • 200 - OK The request has succeeded.
  • 301 - Moved Permanently The document has moved URL here
  • 302 - Found The document has moved URL here
  • 303 - See Other The answer to your request is located URL here
  • 304 - Not Modified A cached copy will be used
  • 400 - Bad Request Your browser sent a request that this server could not understand
  • 401 - Authorization Required This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.
  • 403 - Forbidden You don't have permission to access on this server
  • 404 - Not Found The requested URL was not found on this server
  • 405 - Method Not Allowed The requested method GET is not allowed for the URL
  • 408 - Request Time-out Server timeout waiting for the HTTP request from the client
  • 410 - Gone The requested resource is no longer available on this server and there is no forwarding address. Please remove all references to this resource
  • 411 - Length Required A request of the requested method GET requires a valid Content-length
  • 412 - Precondition Failed The precondition on the request for the URL evaluated to false
  • 413 - Request Entity Too Large The requested resource does not allow request data with GET requests, or the amount of data provided in the request exceeds the capacity limit
  • 414 - Request-URI Too Large The requested URL's length exceeds the capacity limit for this server
  • 415 - Unsupported Media Type The supplied request data is not in a format acceptable for processing by this resource.
  • 500 - Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request
  • 501 - Method Not Implemented GET to URL not supported
  • 502 - Bad Gateway The proxy server received an invalid response from an upstream server
  • 503 - Service Temporarily Unavailable The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
  • 506 - Variant Also Negotiates A variant for the requested resource URL is itself a negotiable resource. This indicates a configuration error.
User avatar
produke
 
Posts: 242
Joined: 25 Sep 2006 04:48


Re: htaccess ErrorDocument Examples

Postby srivalli » 04 Sep 2008 05:39

Regarding 301 status. I written in htaccess file as
Code: Select all
ErrorDocument 301 /projectname/folder1/folder2/301.php


How i can test 301 error status in my localhost. How to reproduce that error status.
Actually i have to display some static page when i am getting error codes like 301/401/501...

Whether that static page should be in project folder or out of that folder?
when i am trying to authenticate, then that page is in project folder. its not accessing that page when i am getting 401 error.
Is it access when 301 error also will come? 301 is a moved permanently status code, so where i have to place it in our project, similarly 501 error..

Some one guide me regarding this...

Thanks,
Srivalli Ch.
srivalli
 
Posts: 2
Joined: 04 Sep 2008 05:24


Return to Main



Who is online

Users browsing this forum: No registered users