I'm developing a PHP app with the following structure:
my-app/
WEB/
css/
images/
javascript/
index.php
.htaccess
lib/
my-app/
...
config.php
The web server is pointed to the WEB
directory and lib
is in PHP's include_path
.
mod_rewrite
is used to rewrite requests to missing files to index.php
, which loads a RequestDispatcher
class from lib
to do the processing.
In previous apps I've been using a configuration file which stores the application base URL path, e.g.:
$CFG['app']['context_path'] = '/my-app';
Now I'm developing a web app which should be available at multiple URL paths at the same time from one codebase - e.g. in /usr/share/my-app
and multiple domains can use it from there and map it to freely choosen path by e.g. symlinking, Alias
-ing, etc .
There are multiple propsed solutions on the net but none of the ones I've found works reliably.
The proposed solution should work in all PHP web execution environments:
when the URL path mapping is done via:
DocumentRoot /usr/share/my-app/WEB
, path: ''DOCROOT/my-app -> /usr/share/my-app/WEB
, path: /my-appAlias
-ing - Alias /my-app/ /usr/share/my-app/WEB/
, path: /my-appmod_userdir
- /home/USER/public-html/my-app -> /usr/share/my-app/WEB
, path: /~USER/my-appI've developed the following:
$app_base_url_path = str_replace( '/' . \basename(__FILE__), '', $_SERVER['PHP_SELF'] );
which, when put in WEB/index.php
, works in common cases but still has some issues in some of the tested scenarios. E.g:
/home/USER/public_html
( where mod_userdir
is also pointed to ) and he app is accessed as /~USER/my-app/
, the path is returned as just /my-app
(where its also available BTW), which is incorrect and can cause problems when used for e.g. a COOKIE path. This might be Apache HTTPd bug though, as $_SERVER['CONTEXT_PREFIX']
is empty, while it should be /~USER
IMHO.I've also had thoughts on making a subrequest to a predefined known return URL part:
/my-app/abc/efg/g/m?d=5&fff=8443#5
/my-app/abc/efg/g/MY-KNOWN-RVAL
/my-app/abc/efg/MY-KNOWN-RVAL
/my-app/abc/MY-KNOWN-RVAL
/my-app/MY-KNOWN-RVAL
until the known result is returned. This might be usable for some apps, but has too much overhead for mine.
My tests were done with PHP 7.3 running as mod_php on Apache 2.4.38 on Debian 10 (Buster).
P.S.I'm also interested in results in different environments(including other web servers), espesially ones which are not too common. If you find this working or not please post a comment with your environment details and results.
References: