Difference between revisions of "Htaccess: Site error documents"

From The Uniform Server Wiki
Jump to navigation Jump to search
(New page: <span id="top"></span> <div style="padding:0;margin:0; border-bottom:3px inset #000000"> {| | MPG UniCenter || .htaccess: [[Htaccess: Introduction | Introduc...)
 
m (Folder to Directory.)
 
(3 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
<div style="padding:0;margin:0; border-bottom:3px inset #000000">
 
<div style="padding:0;margin:0; border-bottom:3px inset #000000">
 
{|  
 
{|  
|[[Image:uc_small_logo.gif | MPG UniCenter]] ||
+
 
 
.htaccess:  
 
.htaccess:  
 
[[Htaccess: Introduction | Introduction]] |   
 
[[Htaccess: Introduction | Introduction]] |   
Line 20: Line 20:
 
'''Site error documents'''
 
'''Site error documents'''
  
Apache provides some bland error documents however you are not restricted to using these you can provide your own and match them to your sites look.
+
Apache provides some bland error documents, but you are not restricted to using just these. You can provide your own and match them to your site's look.
  
== Whats errors produce what ==
+
== What errors produce what ==
The following are a list of the Apache error numbers and what they relate to:
+
The following is a list of the Apache error numbers and what they relate to:
  
 
{|cellpadding="2" cellspacing:4px style="font-size:11px"
 
{|cellpadding="2" cellspacing:4px style="font-size:11px"
Line 33: Line 33:
 
|202 Accepted||style="background:#f0f0f0"|401 Authorization Required||501 Not Implemented
 
|202 Accepted||style="background:#f0f0f0"|401 Authorization Required||501 Not Implemented
 
|-
 
|-
|203 Non-Authorative Information||style="background:#f0f0f0"|403 Forbidden||502 Bad Gateway
+
|203 Non-Authoritative Information||style="background:#f0f0f0"|403 Forbidden||502 Bad Gateway
 
|-
 
|-
 
|204 No Content||style="background:#f0f0f0"|404 Not Found||503 Service Unavailable
 
|204 No Content||style="background:#f0f0f0"|404 Not Found||503 Service Unavailable
Line 60: Line 60:
 
|}
 
|}
 
   
 
   
'''''Note'':''' There is no need to create error pages for all the above, following are worth considering: 401, 403, 404 and 500
+
'''''Note'':''' There is no need to create error pages for all the above. The following are worth considering: 401, 403, 404 and 500
  
'''''[[#top | Top]]'''''
 
 
== How to create customised error documents ==
 
== How to create customised error documents ==
Create a new folder in your root folder (www) and name it '''errors'''. This folder contains all the error pages you wish to display. For example '''not_found.html''' this will be displayed when a page cannot be found on your web site.
+
Create a new Directory in your root directory (www) and name it '''errors'''. This will contain all the custom error pages you wish to display. For example, '''not_found.html''' will be displayed when a page cannot be found on your web site.
  
Now open the root htaccess file (the one in your root folder www) and add the following line:
+
Now open the root .htaccess file (the one in your root directory www) and add the following line:
  
 
'''ErrorDocument 404 /errors/not_found.html'''
 
'''ErrorDocument 404 /errors/not_found.html'''
  
Save the file. The command '''ErrorDocument 404''' will forward a user to '''yoursite/errors/not_found.html''' file when ever Apache produces the error 404.
+
Save the file. The command '''ErrorDocument 404''' will forward a user to the '''<root>/errors/not_found.html''' file when ever Apache produces the error 404.
  
'''''Note 1'':''' The files and folder can be named anything you like.<br>
+
'''''Note 1'':''' The files and directory can be named anything you like.<br>
'''''Note 2'':''' You can use a full URL (as oppose to a virtual path) to your error file for example: <nowiki>http://yoursite.com/errors/not_found.html</nowiki>
+
'''''Note 2'':''' You can use a full URL (as oppose to a virtual path) to your error file; for example: <nowiki>http://yoursite.com/errors/not_found.html</nowiki>
  
If you create error pages for the above your htaccess file will look similar to this when using virtual paths:
+
If you create error pages for the above, your .htaccess file will look similar to this when using virtual paths:
  
:* ErrorDocument 401 /errors/auth_reqired.html
+
:* ErrorDocument 401 /errors/auth_required.html
 
:* ErrorDocument 403 /errors/forbidden.html
 
:* ErrorDocument 403 /errors/forbidden.html
 
:* ErrorDocument 404 /errors/not_found.html
 
:* ErrorDocument 404 /errors/not_found.html
Line 84: Line 83:
 
Or look like similar to this if when using full URL paths:
 
Or look like similar to this if when using full URL paths:
  
:*  ErrorDocument 401 <nowiki>http://yoursite.com/errors/auth_reqired.html</nowiki>
+
:*  ErrorDocument 401 <nowiki>http://yoursite.com/errors/auth_required.html</nowiki>
 
:*  ErrorDocument 403 <nowiki>http://yoursite.com/errors/forbidden.html</nowiki>
 
:*  ErrorDocument 403 <nowiki>http://yoursite.com/errors/forbidden.html</nowiki>
 
:*  ErrorDocument 404<nowiki> http://yoursite.com/errors/not_found.html</nowiki>
 
:*  ErrorDocument 404<nowiki> http://yoursite.com/errors/not_found.html</nowiki>
 
:*  ErrorDocument 500 <nowiki>http://yoursite.com/errors/server_error.html</nowiki>
 
:*  ErrorDocument 500 <nowiki>http://yoursite.com/errors/server_error.html</nowiki>
  
'''''[[#top | Top]]'''''
+
 
 
== Example ==
 
== Example ==
The following shows a little more detail first create the error page to be displayed
+
The following shows a little more detail. First create the error page to be displayed:
  
 
{|
 
{|
Line 99: Line 98:
 
|-style="background:#f9f9f9"
 
|-style="background:#f9f9f9"
 
|
 
|
# Create a new folder in www named '''errors'''.
+
# Create a new directory in www named '''errors'''.
# In this folder create a file named '''not_found.html'''&nbsp;
+
# In this directory create a file named '''not_found.html'''&nbsp;
# Add the code shown on the right to it.
+
# Add the code shown on the right.
# Save the file
+
# Save the file.
 
|
 
|
 
<pre style="padding:4px;margin:0;border:none">
 
<pre style="padding:4px;margin:0;border:none">
Line 108: Line 107:
 
  <title>Not found</title>
 
  <title>Not found</title>
 
   <body>
 
   <body>
     <p>Example of a not found page</p>
+
     <p>Example of a <b>not found</b> page</p>
 
   </body>
 
   </body>
 
</html>
 
</html>
Line 120: Line 119:
 
|-style="background:#f9f9f9"
 
|-style="background:#f9f9f9"
 
|
 
|
# Navigate to the root folder '''www'''.  
+
# Navigate to the root directory '''www'''.  
# Open the '''.htaccess''' file
+
# Open the '''.htaccess''' file.
 
# Add the command shown on the right last line.   
 
# Add the command shown on the right last line.   
# Save the file
+
# Save the file.
 
|
 
|
 
<pre style="padding:4px;margin:0;border:none">
 
<pre style="padding:4px;margin:0;border:none">
# This file provides security to the server limiting access to the localhost only.
+
# This file provides security for the server, limiting access to the localhost only.
 
# Comment to deactivate.
 
# Comment to deactivate.
  
Line 132: Line 131:
 
Deny from all
 
Deny from all
 
Allow from 127.0.0.1
 
Allow from 127.0.0.1
 +
Allow from ::1
  
# To allow execution of cgi scripts in this directory uncomment next two lines.&nbsp;
+
# To allow execution of cgi scripts in this directory, uncomment the next two lines.&nbsp;
  
 
AddHandler cgi-script .pl .cgi
 
AddHandler cgi-script .pl .cgi
Line 152: Line 152:
 
=== Testing ===
 
=== Testing ===
  
:# Run the server using Server_Start.bat
+
:# (Re)start the server.
:# In the browser address bar type '''<nowiki>http:/localhost/fred.html</nowiki>''' or some other file that does not exist.
+
:# In the browser address bar, type '''<nowiki>http:/localhost/fred.html</nowiki>''' or some other file that does not exist.
 
:# Apache will generate the error code 404 and redirect to your not_found.html page
 
:# Apache will generate the error code 404 and redirect to your not_found.html page
  
Experiment with the ErrorDocument command and create a few pages.
+
Experiment with the ErrorDocument command and create a few other pages.
 
   
 
   
'''''[[#top | Top]]'''''
+
 
 
----
 
----
  
{|
+
[[Category: Apache Configuration]]
| [[Image:uc_small_logo.gif]] || [[User:Ric|Ric]]
 
|}
 
 
 
[[Category: UniCenter]]
 
[[Category: Support]]
 
[[Category: Troubleshooting ]]
 
[[Category: Application]]
 
[[Category: Development]]
 

Latest revision as of 11:01, 21 June 2013

.htaccess: Introduction | Site error documents | Prevent Directory Listing | Redirect | Preventing hot linking |

.htaccess - Apache directory-level configuration file

Site error documents

Apache provides some bland error documents, but you are not restricted to using just these. You can provide your own and match them to your site's look.

What errors produce what

The following is a list of the Apache error numbers and what they relate to:

Successful Client Requests Client Request Errors Server Errors
201 Created 400 Bad Request 500 Internal Server Error
202 Accepted 401 Authorization Required 501 Not Implemented
203 Non-Authoritative Information 403 Forbidden 502 Bad Gateway
204 No Content 404 Not Found 503 Service Unavailable
205 Reset Content 405 Method Not Allowed 504 Gateway Timeout
206 Partial Content 406 Not Acceptable (encoding) 505 HTTP Version Not Supported
Client Request Redirected 407 Proxy Authentication Required  
300 Multiple Choices 408 Request Timed Out  
301 Moved Permanently 409 Conflicting Request  
302 Moved Temporarily 410 Gone  
303 See Other 411 Content Length Required  
304 Not Modified 412 Precondition Failed  
305 Use Proxy 413 Request Entity Too Long  
  414 Request URI Too Long  
  415 Unsupported Media Type  

Note: There is no need to create error pages for all the above. The following are worth considering: 401, 403, 404 and 500

How to create customised error documents

Create a new Directory in your root directory (www) and name it errors. This will contain all the custom error pages you wish to display. For example, not_found.html will be displayed when a page cannot be found on your web site.

Now open the root .htaccess file (the one in your root directory www) and add the following line:

ErrorDocument 404 /errors/not_found.html

Save the file. The command ErrorDocument 404 will forward a user to the <root>/errors/not_found.html file when ever Apache produces the error 404.

Note 1: The files and directory can be named anything you like.
Note 2: You can use a full URL (as oppose to a virtual path) to your error file; for example: http://yoursite.com/errors/not_found.html

If you create error pages for the above, your .htaccess file will look similar to this when using virtual paths:

  • ErrorDocument 401 /errors/auth_required.html
  • ErrorDocument 403 /errors/forbidden.html
  • ErrorDocument 404 /errors/not_found.html
  • ErrorDocument 500 /errors/server_error.html

Or look like similar to this if when using full URL paths:

  • ErrorDocument 401 http://yoursite.com/errors/auth_required.html
  • ErrorDocument 403 http://yoursite.com/errors/forbidden.html
  • ErrorDocument 404 http://yoursite.com/errors/not_found.html
  • ErrorDocument 500 http://yoursite.com/errors/server_error.html


Example

The following shows a little more detail. First create the error page to be displayed:

Comment Code
  1. Create a new directory in www named errors.
  2. In this directory create a file named not_found.html 
  3. Add the code shown on the right.
  4. Save the file.
<html>
 <title>Not found</title>
  <body>
    <p>Example of a <b>not found</b> page</p>
  </body>
</html>

Add the command to htaccess:

Comment Code
  1. Navigate to the root directory www.
  2. Open the .htaccess file.
  3. Add the command shown on the right last line.
  4. Save the file.
# This file provides security for the server, limiting access to the localhost only.
# Comment to deactivate.

Order Deny,Allow
Deny from all
Allow from 127.0.0.1
Allow from ::1

# To allow execution of cgi scripts in this directory, uncomment the next two lines. 

AddHandler cgi-script .pl .cgi
Options +ExecCGI

# To unlock your server, comment the next 4 lines.
# Defaults: Username = admin; Password = userver

#AuthName "Uniform Server - Secure Server Access"
#AuthType Basic
#AuthUserFile /htpasswd/www/.htpasswd
#Require valid-user

ErrorDocument 404 /errors/not_found.html

Testing

  1. (Re)start the server.
  2. In the browser address bar, type http:/localhost/fred.html or some other file that does not exist.
  3. Apache will generate the error code 404 and redirect to your not_found.html page

Experiment with the ErrorDocument command and create a few other pages.