404: Difference between revisions

From Freephile Wiki
Created page with "Tumbeasts sign1|200px|right 404 is the server response code for "Document Not Found". You can do some interesting things with your server to pro..."
 
m Text replacement - "<(\/?)source" to "<$1syntaxhighlight"
 
(2 intermediate revisions by the same user not shown)
Line 6: Line 6:


Here's a simple example just redirects a person to your homepage for any request that would otherwise be 'not found'.  It's not actually all that helpful.
Here's a simple example just redirects a person to your homepage for any request that would otherwise be 'not found'.  It's not actually all that helpful.
<source lang="apache">
<syntaxhighlight lang="apache">
ErrorDocument 404 /
ErrorDocument 404 /
</source>
</syntaxhighlight>


Here's a more interesting example for using an ErrorDocument in a virtual hosting scenario where you want to handle not just missing documents, but also 'missing' domains.
Here's a more interesting example for using an ErrorDocument in a virtual hosting scenario where you want to handle not just missing documents, but also 'missing' domains.
The challenge: If there's no DocumentRoot, where will the script live?  <code>[https://httpd.apache.org/docs/current/mod/mod_alias.html#alias Alias]</code> to the rescue.
The challenge: If there's no DocumentRoot, where will the script live?  <code>[https://httpd.apache.org/docs/current/mod/mod_alias.html#alias Alias]</code> to the rescue.


<source lang="apache">
<syntaxhighlight lang="apache">
<VirtualHost *>
<VirtualHost *>
   UseCanonicalName Off
   UseCanonicalName Off
Line 21: Line 21:
   ErrorDocument 404 /errors/notfound.php
   ErrorDocument 404 /errors/notfound.php
</VirtualHost>
</VirtualHost>
</source>
</syntaxhighlight>
Now, when an unconfigured domain is requested, the script at /var/www/default/errors/notfound.php is invoked instead. This script can check <code><nowiki>$_SERVER['HTTP_HOST']</nowiki></code> to see what domain was requested. If it is actually configured, then we have a regular 404. If it's not configured, we can display some alternate error message. You could redirect to a "default" domain, or show a sign-up screen for your hosting service.<ref>http://serverfault.com/a/685521/192385</ref>
Now, when an unconfigured domain is requested, the script at /var/www/default/errors/notfound.php is invoked instead. This script can check <code><nowiki>$_SERVER['HTTP_HOST']</nowiki></code> to see what domain was requested. If it is actually configured, then we have a regular 404. If it's not configured, we can display some alternate error message. You could redirect to a "default" domain, or show a sign-up screen for your hosting service.<ref>http://serverfault.com/a/685521/192385</ref>


{{References}}
{{References}}

Latest revision as of 13:18, 24 February 2025

Tumbeasts sign1
Tumbeasts sign1

404 is the server response code for "Document Not Found".

You can do some interesting things with your server to provide a better (or amusing) experience to the user[1].

Here's a simple example just redirects a person to your homepage for any request that would otherwise be 'not found'. It's not actually all that helpful.

ErrorDocument 404 /

Here's a more interesting example for using an ErrorDocument in a virtual hosting scenario where you want to handle not just missing documents, but also 'missing' domains. The challenge: If there's no DocumentRoot, where will the script live? Alias to the rescue.

<VirtualHost *>
  UseCanonicalName Off
  VirtualDocumentRoot /var/www/clients/%-2/%0/
  ServerName catchall.host
  Alias /errors /var/www/default/errors/
  ErrorDocument 404 /errors/notfound.php
</VirtualHost>

Now, when an unconfigured domain is requested, the script at /var/www/default/errors/notfound.php is invoked instead. This script can check $_SERVER['HTTP_HOST'] to see what domain was requested. If it is actually configured, then we have a regular 404. If it's not configured, we can display some alternate error message. You could redirect to a "default" domain, or show a sign-up screen for your hosting service.[2]

References[edit]