An alternative way to another answer would be to simply define the relative path in each file.
Assuming the folder structure is:
+-- ./
+-- conexao
+-- conexao.php
+-- pasta1
+-- exemplo1.php
+-- pasta2
+-- exemplo2.php
+-- pasta3
+-- exemplo3.php
In the file pasta1/exemplo1.php
:
<?php
require '../conexao/conexao.php';
In the file pasta1/pasta2/exemplo2.php
:
<?php
require '../../conexao/conexao.php';
In the file pasta1/pasta2/pasta3/exemplo3.php
:
<?php
require '../../../conexao/conexao.php';
You can also create an index.php file along with .htaccess and it would access any file.
The file should look something like .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule (.*) index.php/$1 [QSA,L]
</IfModule>
And index.php
should look like this:
<?php
require 'conexao/conexao.php';
$pathInfo = isset($_SERVER['PATH_INFO']) $_SERVER['PATH_INFO'] : '';
$pathInfo = ltrim($pathInfo, '/');
if (empty($pathInfo)) {
echo 'Index normal';
} else if (is_file($pathInfo)) {
require $pathInfo;
} else {
header('X-PHP-Response-Code: 404', true, 404);
echo 'Página não encontrada';
}