Set Custom Error 403 Page

Asked

Viewed 5,034 times

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 properly, but when it blocks the listing of directories, or php files, it creates a 403 error that gets out of 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 didn’t work.

4 answers

6


According to its documentation, the ErrorDocument apache is very flexible and has 4 ways to redirect and display an error message. Learn:

  1. Display a simple fixed message (hardcoded)
  2. Display a custom message
  3. Redirect internally to a local page to manipulate the error or problem
  4. Redirect to an external address handle the error or problem

The first option consists of the standard error page.

The custom message consists of defining 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 question, allows you to redirect to a page on local file systems. Note that the path must be a relative URL relative to the DocumentRoot and must start with a bar /.

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

The last option is to redirect to an external URL, for example:

ErrorDocument 500 http://foo.example.com/cgi-bin/tester
  • 1

    The problem is that the index.php was not in the DocumentRoot. I added the right path, solved.

5

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

  • Try placing an invalid line anywhere on .htaccess and see if you get a Internal Server Error in the browser. Otherwise, your file is being ignored.
  • In the configuration of VirtualHost, note that the option AllowOverride None causes the .htaccess is ignored by Apache. Then review the configuration of VirtualHost and put 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 permission. I’ve seen a lot of people indicating 644;

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

  • Just for the record, you don’t need to restart Apache to see modifications to .htaccess (this is the idea of .htaccess hehe)

Answering your question

Simple error message. I inserted the next line into my .htaccess:

ErrorDocument 403 "Acesso negado!"

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

Page with error message. I inserted the next line into my .htaccess.

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

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

Unresolved

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

ErrorDocument 403 /403.html

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

Additionally, a 404 Not Found error was encountered while trying to use an Errordocument to Handle the request.

Some references (all in English):

UPDATED 17/01/2014, 13:53

I tested again the configuration below and ran smoothly:

ErrorDocument 403 /403.html

NOTE: As a basis, I used exactly the same .htaccess that you published.

4

-1

In my case it had not been clear that the directory containing the new error pages is always below the main one. For example, in the case of Apache the directory is below [ /public_html ]. The directory [ dir ] of the answer 4 is below it. There, any . htaccess in any folder of the site will be automatically referencing this [ dir ] that is under [ /public_html ]. A very common deception is to create a directory of error pages under a folder that contains new content ( e.g., /public_html/wordpress/dir). It’s actually in the root folder !

  • you used the option to send a reply, however it seems that your intention was just to comment on experience with this same problem. In this case consider adding a Comment instead of a Reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.