Search code examples
php.htaccessdir

Get the Folder in which the index.php script is Php


I am using AltoRouter to match routes for my php app. Apparently even if index.php is called, if the app is not in the domain root, you have to set $router->setBasePath("/path/to/script");

I need a function that collect the subfolder(if app is in subfolder).Eg if path is /dir and someone calls www.domain.com/dir, i can know its /dir and call $router->setBasePath("/dir");


Solution

  • You can use this rule in your .htaccess:

    RewriteEngine On
    
    RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)\1$
    RewriteRule ^.*$ - [E=BASE:%2]
    

    This will set `${ENV:BASE}" to whatever sub-directory this .htaccess is residing in.

    Then inside PHP you can use:

    $_SERVER["BASE"]
    

    which will be set to /dir/ if .htaccess is in /dir1/.