views:

20

answers:

2

How to detect root folder in URI when using apache mod_alias and mod_rewrite?

I have an website running on a sub folder in of the document root for example /var/www/this/an/other/dir in this folder I have a .ht_access file with mod_rewrite rules to redirect everything to index.php

# Do nothing if the file (s), link (l) or (d) exists
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

# Redirect everything else to index.php
RewriteRule ^.*$ index.php [E=PORT:%{SERVER_PORT},NC,L]`

I also have set up an alias for easy access

alias /foo /var/www/this/an/other/dir

Now when I go to the address http://host.com/foo/bar/ how can I detect that /foo is the root folder of my URI?

A: 

I don't quite understand your question.
root folder is always the same - /. No need to determine it.
If you need virtual document_root - just use dirname($_SERVER["SCRIPT_FILENAME"])

Col. Shrapnel
What I want to know is when I'm in /foo/bar that my index.php is in /foo
vdrmrt
$_SERVER["SCRIPT_FILENAME"] returns the path from the server and not from the uri
vdrmrt
@vdrmrt yes, `dirname($_SERVER["SCRIPT_FILENAME"])` is your virtual document root. So I said. As for the foo - I don't know why do you need that, but you can so some magic by comparing `SCRIPT_FILENAME` with `DOCUMENT_ROOT`. `phpinfo(32)` can give you some hints.
Col. Shrapnel
comparing SCRIPT_FILENAME with DOCUMENT_ROOT is not reliable, since you can very well have `Alias /foo /var/www/some/dir/bar/`, then you're screwed.
Geoffrey Bachelet
+1  A: 

Easiest way is to set an env variable directly from within Apache:

SetEnv AliasRoot /foo

You can then access it through the $_ENV superglobal:

$root = $_ENV['AliasRoot'];

I don't think there is a reliable way to do this in pure PHP.

Geoffrey Bachelet
That worked, however I accessed it trough $_SERVER. Thanks!
vdrmrt