简体   繁体   中英

How to catch Apache's 403 error in PHP / Kohana?

My website is running on Kohana v3, and I have set up customized error page like below in bootstrap.php

try
{
    // Attempt to execute the response
    $request->execute();
}
catch (Exception $e)
{ 
      .... error page re-direct.
}

It works fine with 404 error.

I also have some folders that cannot be directly accessed but they have to be located under the www root. I put the .htaccess to protect these folders

# nobody can access this folder from Browser
Deny from all

When user type in the URL in the browser to access files under this folder, an Apache 403 error will be shown in plain text like this:

Forbidden

You don't have permission to access /private_folder on this server.

I want to customize this error and handle it in Kohana. However, I find that Apache fully controls the 403 error, and does not even call the index.php under this situation.

Any idea to solve it?

Instead of using .htaccess to lock that area down, you could do it in code. PHP's header function could be used to send the 403 status.

Another approach would be to set up a custom error document for 403 errors in Apache, and point this at a script to handle the error. Refer to the ErrorDocument documentation on how to do this, but it would be something along these lines:

ErrorDocument 403 /custom_403_handler.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM