Set custom 403 error page

12

I have the following excerpt in my file .htaccess :

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ index.php?route=$1 [L]
</IfModule>


# Bloqueia todos os arquivos PHP, com exceção do index.
<Files *.php>
    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1
</Files>

<Files index.php>
    Order Allow,Deny
    Allow from all
</Files>

# Bloqueia a listagem de diretórios.
Options All -Indexes

It works correctly, but when it locks the directory listing, or php files, it creates a 403 error that falls outside the default layout.

Question

How can I "forward" these 403 errors to the file index.php ?

I tried to add the code:

ErrorDocument 403 /index.php

But it did not work.

    
asked by anonymous 17.01.2014 / 07:11

3 answers

5

According to its documentation, apache's directive ErrorDocument is very flexible and has 4 ways to redirect and display an error message. Namely:

  • Display a simple fixed message ( hardcoded )
  • Display a custom message
  • Redirect internally to a local page handling the error or problem
  • Redirect to an external address handle the error or problem
  • The first option is the default error page.

    The custom message is to define a text to be displayed and can be configured as follows:

    ErrorDocument 403 "Sorry can't allow you access today"
    

    The third option, which is the case of the question, allows you to redirect to a page on the local file systems. Note that the path must be relative URL relative to DocumentRoot and should start with a /

    ErrorDocument 404 /cgi-bin/bad_urls.pl
    ErrorDocument 401 /subscription_info.html
    

    The last option is to redirect to an external URL whose example is:

    ErrorDocument 500 http://foo.example.com/cgi-bin/tester
    
        
    17.01.2014 / 11:22
    5

    For the unsuspecting (like me): make sure your .htaccess is being interpreted.

    • Try placing an invalid line anywhere in .htaccess and see if you get an Internal Server Error in the browser. Otherwise, your file is being ignored.
    • In the VirtualHost setting, note that the AllowOverride None option causes .htaccess to be ignored by Apache. Then review the VirtualHost setting and set AllowOverride All . For example, in /etc/apache2/sites-available/default :
    <VirtualHost *:80>
    # ...
        <Directory /var/www/pasta-do-meu-projeto/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                # AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
    # ...
    </VirtualHost>
    
    • Remember to restart Apache after modifying this:)

      $ sudo service apache2 restart

    • Check the file's permission. I've seen lots of people indicating 644 ;

      $ chmod 644 /var/www/pasta-do-meu-projeto/.htaccess

    • Just to note, it is not necessary to restart Apache to see changes to .htaccess (this is the idea of .htaccess hehe)

    Answering your question

    Simple error message. I inserted the following line in my .htaccess :

    ErrorDocument 403 "Acesso negado!"
    

    I created an empty folder, tried to access it, and received the expected error message.

    Page with error message. I inserted the following line in my .htaccess .

    ErrorDocument 403 http://localhost/pasta-do-meu-projeto/403.html
    

    I created an empty folder, tried to access it and saw the page with an error expected.

    Not yet solved

    For some reason, for error 403 , the local file does not work.

    ErrorDocument 403 /403.html
    

    I created an empty folder, tried to access it and received the following apache message:

      

    Additionally, a 404 Error was encountered while trying to use an ErrorDocument to handle the request.

    Some references (all in English):

    UPDATE ON 01/17/2014, 13:53

    I re-tested the configuration below and it worked without problems:

    ErrorDocument 403 /403.html
    

    NOTE: As a basis, I've used exactly the same% w / w as you posted.

        
    17.01.2014 / 13:02
    4

    Just pass the file path:

    ErrorDocument 403 /dir/file.html
    

    Other reference: link

        
    17.01.2014 / 10:52