Search code examples
phpmod-rewriteapache2url-routing

Rewriting Apache2 paths to look in a subdirectory before using a FallbackResource located in the DocumentRoot


So this piece of software has a piece of nginx configuration that looks like this:

location / {
    try_files /static$uri /router.php?$args;
}
location /router.php {
    include snippets/fastcgi-php.conf;
}

Unfortunately, it seems like this might just be impossible to achieve with Apache2 without disregarding the actual functionality, which poses a problem if one wants to deploy the software on say, a shared web hosting environment where direct access to the server isn't available...

(the project in question is 'Cirrusboard' by ROllerozxa and SN76489/Compa...)

Currently, we (me and the main developer) got Apache to sort of work with the new router. FallbackResource router.php

however, this breaks all static assets as they are located in static/.

I'm not sure how to do this without creating a RewriteRule, which when I tried to, didn't work, it just kept throwing 500 errors. Besides it also doesn't replicate the functionality 1:1 with nginx of first looking to see if a file exists in static/ before even considering router.php at all. Putting the DocumentRoot as the static directory is probably going to break things too since I don't think you can do FallbackResource with a relative and/or absolute path (especially if we take into consideration anything that isn't self-managed hosting like a shared host, something our project very much is taking into consideration).


Solution

  • Our main coder (rollerozxa) figured it out thanks in part to Kazz/7028432's comment on here

    Options -Indexes
    DirectoryIndex this-file-shouldnt-exist.txt
    
    RewriteEngine on
    
    RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI} -f
    RewriteRule ^(.*)$ /static/$1 [END]
    
    FallbackResource /router.php
    

    Thanks very much and hope somebody finds it useful in the future x3