Search code examples
.htaccesscakephp-3.x

CakePHP 3 htaccess ignore directory


After a lot of research I'm still struggling.

My ./.htaccess file looks like this:

<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{REQUEST_URI} !^/trackparser/.*$

    RewriteRule    ^(\.well-known/.*)$ $1 [L]
    RewriteRule    ^$    webroot/    [L]
    RewriteRule    (.*) webroot/$1    [L]
</IfModule>

However, if I go to www.mysite.com/trackparser it just gives me a missing controller error.

How do I make it ignore the trackparser directory?


Solution

  • Besides the required / as mentioned by @GregSchmidt, rewrite conditions only apply to the first rule that is following them, so your config basically says:

    IF (URI != ^/trackparser/.*$) {
        RewriteRule ^(\.well-known/.*)$ $1 [L]
    }
    
    RewriteRule ^$    webroot/  [L]
    RewriteRule (.*) webroot/$1 [L]
    

    What you want is to apply all three rules if the URI doesn't match ^/trackparser/.*$. To achieve that you can invert the condition and use a skipping rule, which when applied, will skip N number of the subsequent rules:

    RewriteRule ^(\.well-known/.*)$ $1 [L]
    
    RewriteCond %{REQUEST_URI} ^/trackparser(/.*|$)
    RewriteRule . - [S=2]
    
    RewriteRule ^$   webroot/   [L]
    RewriteRule (.*) webroot/$1 [L]
    

    This will skip the 2 CakePHP webroot rules in case the condition is met, so it's basically doing:

    RewriteRule ^(\.well-known/.*)$ $1 [L]
    
    IF (URI != ^/trackparser(/.*|$)) {
        RewriteRule ^$   webroot/   [L]
        RewriteRule (.*) webroot/$1 [L]
    }
    

    Note the removed negation operator, and the added (/.*|$) to make the ending slash optional. The rule for the well-known directory (certificate validation) could theoretically also be skipped, but I'd make it exempt in order to avoid it accidentally not being applied because of later changes.

    See also