Run a custom error page in the same sub directory as the htacess

durangod

Well-Known Member
May 12, 2012
504
46
78
cPanel Access Level
Website Owner
Hi,
I know that htaccess modrewrite is based on document root. Here is my path to my custom error files

example.com/subdir has its own htaccess and its own 403.shtml file (i have the 404 taken care of with some fancy modrewrite so i dont need the 404)

But i need the 403, at first i thought i could use the htaccess <if) statment here


and just check to see if the response was 403, then redirect but the command data i need to use, the data is not available for the <If> statement :(

so what i need to do is find a way to use ErrorDocument to run in a sub directory for which i do not know the name (this is for a script that i am working on and i wont know the domain its being install on)

So how do i take this in htaccess

ErrorDocument 403 /403.shtml (this uses the 403 file in the directory above - not what i need)

and make the server see this

ErrorDocument 403 [current file directory] 403.shtml


htaccess does have a <If> statement so i dont know why i cant do something like this to avoid the whole doc root issue with their requirement of /

Code:
<If ErrorDocument 403>
Redirect .* 403.shtml
</If>
if that could work somehow then it would use the 403.shtml file in the same dir as the htaccess But it just gives a server error 500, wont work... :(



Thanks
 
Last edited:

durangod

Well-Known Member
May 12, 2012
504
46
78
cPanel Access Level
Website Owner
After much reading apache docs {and we all know how much torchure that is lol) and testing and testing there is just no way to do this for a 403 via mod_rewrite.
First of all the htaccess if statment is not even supported for the {RESPONSE} header. So i looked at two over variables, the if statement is not supported with them either. There were just so many dead ends that i finally did this..

Is it hacky, yes
Is is something i am proud of, no
does it do the job, yes
do i hope to change it for another solution asap, yes
if someone has a better way i would welcome it, yes

So here is how i was able to use a local htacces file and a local 403 custom error file in the same dir. You have to do a double redirect its the only way i know of to do it and keep the url dynamic. Since it has to start with a / and its based off of the document root, then it really limits options. Its hard for me to believe that in this day and age that apache still has no way of telling you via htacess what your current working directory is, the ony way to get it is to get the full REQUEST_URI and filter it out, pretty sad..

Anyway here is the process i came up with, the only thing i was not able to do (yet) is to make the url dynamic or just load the local index with /index.php...
Basically what we are doing is just a handoff..

First you add this to your htaccess

Code:
#403 we send them to the index
#and then send them to the local error file
#its the only way to do it via htaccess
#for 403  apache conditions cant
#trap 403 other than with this command
ErrorDocument 403 https://example.com/subdir/index.php?er=403

[ICODE]

That will send them to the local index file rather than to the 403 error file

then in the index.php file you set up an error switch control

[CODE]
if(isset($_GET['er']))
{  
    
    $er = (int) $_GET['er'];
    
    switch($er)
    {
         
       case 403:
            
            //403 forbidden error from some file via htaccess
            //this is the only way i found to use a local 403 file
            //in a sub dir is to send them to the index on 403 and
            //then to the local 403 file.  Cant do it via htaccess,
            //i tried many many ways.
           
            ?>
           
            <script type="text/javascript">
             location.replace("403.shtml");
             </script>
 
            <?php
   
            exit;  //always exit after js location
               
       break;

    }//close switch
     
}//close if GET er

And thats it, this sees the 403 error code in the url and sends them to the local (subdir) error file. :)
 
  • Like
Reactions: cPRex