home Get a blog for free contact login

Articles in dir: /blog/tech/

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

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:

  • mod_php
  • FPM
  • Fast CGI
  • other ?

when the URL path mapping is done via:

  • direct installation in a web server public directory - e.g. DocumentRoot /usr/share/my-app/WEB, path: ''
  • symlinking - DOCROOT/my-app -> /usr/share/my-app/WEB, path: /my-app
  • Alias-ing - Alias /my-app/ /usr/share/my-app/WEB/, path: /my-app
  • mod_userdir - /home/USER/public-html/my-app -> /usr/share/my-app/WEB, path: /~USER/my-app
  • other ?

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:

  • when DOCROOT is pointed to /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:


Posted in dir: /blog/tech/

konsole settings and Terminus Bold font

After upgrading from Debian Stretch to Buster I had a problem, where konsole was re-setting its fonts preference.

The font I prefer is Terminus Bold, the bold part was going away though with each restart. It was odd and looked like a bug. I found this problem report:

https://www.reddit.com/r/kde/comments/88hzdb/konsole_bold_font/

It did not help though, so here is what worked in my case:

rm .config/session/konsole_*

Posted in dir: /blog/tech/
Tags: kde konsole

All tags SiteMap Owner Cookies policy [Atom Feed]