6

I would like to do a 404 redirect with Apache, and I find several solutions:

  1. The .htaccess method. But I don't want to add a .htaccess if not necessary.

  2. virtual host method:

<VirtualHost *:80>
    ServerAlias *.example.com
    Redirect 404 /index.html
    ErrorDocument 404 /index.html
</VirtualHost>

I would like to know what's the difference Redirect 404 and ErrorDocument 404?

11

Did you test with Redirect directive?

Redirect 404 /index.html means that a 404 response will return when the client request /index.html (even if it could possibly exist).

ErrorDocument 404 /index.html means that when the client access a non exist URL, Apache will redirect to index.html page.

You must use ErrorDocument in this case.

1
2

The two are generally unrelated. When I just tried to set up a redirect with a 404 status I got the error message Redirect URL not valid for this status when trying to start apache.

A Redirect sends the client to a new address and provides a status for the client. The status returned are usually 30x values.

The ErrorDocument directive configures apache to return a particular page (rather than the default page) when an error of type nnn occurs. In your example you are saying return /index.html when a 404 (Not Found) error occurs.

What are you trying to achieve ?

0

If you configure the ErrorDocument directive correctly, all you have to do is make sure the file is not actually present. If you are trying to match a filename pattern, then you need to match the pattern and rewrite the request to a non-existant response. And make sure to turn off directory browsing:

<VirtualHost *:80>
 Options -Indexes
 ErrorDocument 404 /not-found.htm
 AliasMatch /index\.* /something/not/here
</VirtualHost>
0

These are 3 different things:

  1. Where you configure your directives:

    • in the Apache config (e.g. vhost file, typically in /etc/apache2/sites-available)
    • or in a .htaccess somewhere in the web root.

    Most directives, you can put in either file. The directives in .htaccess always apply to the current directory. You can put them in the web root or in a subdirectory. Changes in the Apache config require a reload of the webserver (which is a good thing because it does some checks too). It is usually recommended to not use .htaccess at all (if you don't have to) for performance reasons.

  2. In the context where you define the directive, if in <VirtualHost *:80> it is in virtual host context (meaning it applies for this virtual host). You can also use Directory context, meaning it applies to a specific directory only. This is explained quite well in the Apache 2.4 "Configuration Sections" docs. .htaccess applies to specific directories (or all if you put it in the top level).

  3. How you configure, meaning which directives. ErrorDocument and Redirect do different things:

    • ErrorDocument defines what should be displayed in the case of an error, use 404 for page not found. The directive you posted above means index.html will be used as error page. You usually don't want that. You usually want a specific error page to be displayed, e.g. ErrorDocument 404 error.html. This is the recommended way to do this: It will return HTTP status code 404 (which is good because it causes search engines to remove page from index) and it will display error.html in the browser.
    • the Redirect statement you posted above will get you a 404 HTTP status code, but will not display the page you specified, so use ErrorDocument.

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.