home Get a blog for free contact login

PHP + Apache : Universal way to get the application base URL path

Posted: Sun, 12 Jan 2020 04:14:17 +0200 (Updated: Mon, 13 Jan 2020 00:38:24 +0200)
Author: Делян Кръстев

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:

I'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:

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:


Posted in dir: /blog/tech/

Show comments Report article PermaLink

All tags SiteMap Owner Cookies policy [Atom Feed]