I already found this script to "change the documentroot":
RewriteEngine on
RewriteCond %{HTTP_HOST} ^your-domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.your-domain-name.com$
RewriteCond %{REQUEST_URI} !new-folder/
RewriteRule (.*) /new-folder/$1 [L]
It's simple and cool, but I want to complement the script. I want to redirect to the file /new-folder/index.php if the requested file does not exist inside the directory /new-folder/, so not only %{REQUEST_URI} !-f
!
But I don't know how to get the current script-path, like __DIR__
(PHP) in .htaccess.
So my question is, how to check if a file exists inside a subdirectory (new-folder)?
My redirect will theoretically look like:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^your-domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.your-domain-name.com$
RewriteCond %{REQUEST_URI} !./new-folder/
RewriteCond (PATH+Filename, if no filename is set, take index.php) !-f
RewriteRule (.*) ./new-folder/index.php [L]
The above code uses ./ to check relative, it does not know if it's inside the root!
Also I would like to manipulate the REQUEST_URI: replace the /new-folder/ from it, so scripts inside this folder don't have to do it
You can use it like this in your root .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?your-domain-name\.com$ [NC]
RewriteCond %{REQUEST_URI} !/new-folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/new-folder/$1 !-f [NC]
RewriteCond %{DOCUMENT_ROOT}/new-folder/$1 !-d [NC]
RewriteRule (.*) /new-folder/index.php [L]
Another option is use this in any sub-directory /anydirectory/.htaccess
:
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}new-folder/$1 !-f [NC]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}new-folder/$1 !-d [NC]
RewriteRule (.*) %{ENV:BASE}new-folder/index.php [L]