Pergunta

I´m having the following problem: I´d like to build a script which should work in every directory, regardless wheter root-directory or subdirectory. So the most important thing is, that I have a .htaccess-file which will redirect all requests to the index.php in the directory where the script was installed. The redirect is simple, there are plenty of tutorials. But the problem is that I don't know the path in which the script was installed. I don't know if it's a subdirectory or root-directory. The only solution I see is to make a install.php which will modify the htaccess with the correct paths. But are there better solutions?

Thank you!

Update: Since there is apparently misunderstandings with my question, I will give you an example. Let's say my project contains only one index.php and a .htaccess-file. The .htaccess-file should redirect all requests to the index.php. That is very simple, BUT: the .htaccess-file does not know in which directory it will be called, so it could be the user installed the project in /xyz/ but also in the root directory /. If I request xyz/abc/123/echo/world/ the .htaccess-file does not know in which of this directories the index.php is for the redirect, it only knows that it should redirect to the index.php of the root of the script. So my solution would be to make a install.php and change the .htaccess-file with the correct paths, but aren't there better solutions?

Foi útil?

Solução

Place this rule in your root .htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?/)?[^/]+$ /$1/index.php [L]

This will rewrite every request that is not a valid file/directory to index.php of that sub-directory.

EDIT: If you want to place this in a /anydir/ then use:

RewriteEngine On

RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . %{ENV:BASE}index.php [L]
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top