Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a website which has three different directories. I want to create a custom error page for each of the different directories. What i did is as follows:

<Directory "directory1">
    ErrorDocument 404 /directory1/error.php
</Directory>

<Directory "directory2">
    ErrorDocument 404 /directory2/error.php
</Directory>

<Directory "directory3">
    ErrorDocument 404 /directory3/error.php
</Directory>

Now, when I am using this code in my root level htaccess, internal server is the result which i am getting. Need help to sort the issue.

share|improve this question
    
It's possible that the PHP file is causing the Internal Server Error. The file that is run when a 404 is encountered causes a 500 error instead. Have you tried the above with just a string instead of a PHP file? "Not found in directory1", etc. –  Ladadadada Sep 27 '12 at 11:51

1 Answer 1

<directory> directive cannot be used in an htaccess file.

Instead you could:

  • Create an htaccess file in each of the subfolders with just the ErrorDocument directive

  • Write some hairy mod_rewrite rules

  • Let PHP do the work.

    /.htaccess

    ErrorDocument 404 /errors.php
    

    /errors.php

    <?php
    $error_include = sprintf('%s/%s/error.php', __DIR__,
        explode('/', pathinfo($_SERVER['REQUEST_URI'], PATHINFO_DIRNAME))[1]);
    
    if (@file_exists($error_include)) {
        include($error_include);
    } else {
        echo 'Not Found: and no error document!';
    }
    
share|improve this answer
    
i tried <IfModule mod_rewrite.c> and it worked for me. Simple. –  user1085059 Oct 10 '12 at 14:58
    
For anyone reading this later. <IfModule... does not "fix" the overall problem. –  h0tw1r3 Oct 10 '12 at 15:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.