WebFaction
Community site: login faq

Hi, I was looking on documentation and this forum how to customize the 404 error (page not found) message on WebFaction, but was not able to find it.

I know that i could manage it by my application with a global redirection in .htaccess but I would prefer to avoid it, because this would cause an overload, obliging my application to check also URI not related to its structure (and not only for invalid id's or parameters values to be parsed inside the URI).

Instead I would prefer that URI not to be intentionally managed by my application (the ones I exclude from my own rewrite because common default names for folders and files as images, js, css, stats, favicon, robots, etc...) to be parsed only by apache and logged into default log.

But I would also like to have the same consistent 404 error page for all pages not found, parsed either by apache or by my application. How can I do it?

Thanks and regards
Roberto

asked 07 Mar '12, 04:48

robertotra
5721221
accept rate: 0%

Could you provide more information about your application? What type of application is it, and how exactly is it supposed to parse 404 error pages?

(07 Mar '12, 04:53) todork

Independently by the application used, I can use a global redirection in htaccess to pass every URI to my main application file:

RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L]

In this way, all URI requests will be managed by my application and from Apache point of view there will be no 404 errors for pages not found (except if I do not have an index.php or robots.txt or favicon.ico file).

But if there are some URI I know from the beginning will not need to be parsed by my application (as example all images in /images folder or if my first segment of acceptable URI will start with letters and not by numbers) I can exclude them from my global redirection in order to reduce to workload for my main controller, by putting first in htaccess:

RewriteRule ^[0-9](.*)$ - [L]
RewriteRule ^images(.*)$ - [L]
[...]

Now, these URI will be managed directly by Apache and not anymore by my main page, and also the 404 error is the one from apache and not a custom error page created by my application. That's why I wanted to know how to customize the Apache 404 error message, for each kind of application I may use.

Just as example, some hosting providers instead of showing a text string, redirect to a 404.html page on the hosting space to be customized by the single user.

(07 Mar '12, 05:15) robertotra

Thanks, this pretty much answers my question (I mainly wanted to know if you are using a PHP/CGI application, or something else).

(07 Mar '12, 06:02) todork

Hi,

You can customize the Apache 404 error page by entering the following directive in your .htaccess:

ErrorDocument 404 /error404.html

(you could use a different filename of course)

permanent link

answered 07 Mar '12, 06:03

todork
1.2k5
accept rate: 34%

Thank you very much, this was just what I was looking for. Is it the same for static apps (only html files) served by Nginx server? Or does it need a different instruction?

(07 Mar '12, 06:31) robertotra

Sorry, this is not possible for static-only apps, as they do not use Apache at all, therefore .htaccess isn't parsed.

(07 Mar '12, 06:42) todork

checking inside the Nginx wiki (http://wiki.nginx.org/HttpCoreModule) i have found the following:

[code]
error_page
syntax: error_page code [ code... ] [ = | =answer-code ] uri | @named_location

default: no

context: http, server, location, if in location

The directive specifies the URI that will be shown for the errors indicated.

Example:

error_page 404 /404.html;
error_page 502 503 504 /50x.html;
error_page 403 http://example.com/forbidden.html;
error_page 404 = @fetch;

Furthermore, it is possible to change the status code of the answer to another, for example:

error_page 404 =200 /empty.gif;
error_page 404 =403 /forbidden.gif;

Additionally you can have your designated error handler determine the returned status code by using = without specifying a status code.

error_page 404 = /404.php;

If there is no need to change URI during redirection it is possible to redirect processing of error pages into a named location:

location / (
error_page 404 @fallback;
)

location @fallback (
proxy_pass http://backend;
)
[/code]

So, a custom error page should be possible to set also for static apps served by Nginx server, isn't it?

(07 Mar '12, 08:45) robertotra

Sorry, but this is not possible in our system. It's possible in Nginx, but we don't support it. It would require rewriting the Nginx configuration file, as it doesn't support local configuration overriding files such as .htaccess for Apache. At this time, I don't think it would be possible to set this up.

(07 Mar '12, 10:16) todork

Thank you for clarifying. I was asking only for completion of information. For me it is not a problem, since my website will not be graphically intensive, so I will leave also static content to be managed by Apache and will have then a custom consistent 404 page for all files.

Anyway, just talking in abstract, even if Nginx does not support local configuration override as apache does by use of .htaccess, I think it would be equally possible to have a custom 404 error page: it is a matter of convention. As you create a default index.html page for each new application, you could create a default 404 page. If users change the document root, they have just to remember to move there(or create there) their 404 error page (otherwise the 404 error will just add a warning about the custom page missing). One general configuration directive (error_page 404 /404.html;) for all websites. Because the location is relative to the web root, each website will resolve to its own 404 page, that can remain the default one or be customized by users. At least, it seems to me so, if I am not wrong.

(07 Mar '12, 12:59) robertotra
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×22

question asked: 07 Mar '12, 04:48

question was seen: 4,282 times

last updated: 07 Mar '12, 12:59

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM