Why does RewriteRule x y.php succeed, but RewriteRule x y.php/foo fail?

I’m dockerizing a web application with existing .htaccess files. When running the application in a debian:stretch with Apache installed, I’m running into issues with RewriteRules.

I’ve boiled it down to an issue that can be illustrated here:

RewriteRule ^works$    /symfony/web/json.php       [QSA,L]
RewriteRule ^fails$    /symfony/web/json.php/user  [QSA,L]

The first RewriteRule sends the user to along to symfony/web/json.php, a file in a Symfony 1 project, where they get an appropriate 404 handled by Symfony.

The second RewriteRule does not send the user into Symfony land, where the trailing "/user" would normally then be given to a controller to handle. I just get a "File not found.".

Instead, it looks like the .htaccess in a deeper directory removes the beginning of the path and send the "/user" back to the .htaccess in the root directory.

Directory structure

root/
├── symfony/
│   ├── web/
│   │   ├── .htaccess
│   │   └── json.php
│   ├── .htaccess
└── .htacces

.htaccesses

root/.htaccess (relevant bits):

RewriteEngine On
RewriteBase /
RewriteRule ^works$    /symfony/web/json.php       [QSA,L]
RewriteRule ^fails$    /symfony/web/json.php/user  [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /symfony/web/index.php [QSA,L]

root/symfony/.htaccess (entire):

RewriteEngine On
RewriteRule ^(.*)$ /symfony/web/$1 [L]

root/symfony/web/.htaccess (relevant bits):

RewriteEngine On
RewriteBase /symfony/

RewriteCond %{REQUEST_URI} ^(/symfony/admin.php)
RewriteCond %{REMOTE_ADDR} !^10.0
RewriteCond %{REMOTE_ADDR} !^10.50
RewriteCond %{REMOTE_ADDR} !^10.60
RewriteCond %{REMOTE_ADDR} !^127.0.0.1$
RewriteCond %{REMOTE_ADDR} !^::1$
RewriteRule .* / [L,R=403]

# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f

# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]

Rewrite logs

To debug, I set LogLevel debug rewrite:trace3. Here are the relevant bits from the log when attempting to go to /fail:

[rewrite:trace3] [rid#7f8c7807d0a0/initial] [perdir /home/wwwroot/root/] strip per-dir prefix: /home/wwwroot/root/fails -> fails
[rewrite:trace3] [rid#7f8c7807d0a0/initial] [perdir /home/wwwroot/root/] applying pattern '^works$' to uri 'fails'
[rewrite:trace3] [rid#7f8c7807d0a0/initial] [perdir /home/wwwroot/root/] strip per-dir prefix: /home/wwwroot/root/fails -> fails
[rewrite:trace3] [rid#7f8c7807d0a0/initial] [perdir /home/wwwroot/root/] applying pattern '^fails$' to uri 'fails'
[rewrite:trace2] [rid#7f8c7807d0a0/initial] [perdir /home/wwwroot/root/] rewrite 'fails' -> '/symfony/web/json.php/user'
[rewrite:trace2] [rid#7f8c7807d0a0/initial] [perdir /home/wwwroot/root/] trying to replace prefix /home/wwwroot/root/ with /
[rewrite:trace2] [rid#7f8c7807d0a0/initial] [perdir /home/wwwroot/root/] trying to replace context docroot /home/wwwroot/root with context prefix 
[rewrite:trace1] [rid#7f8c7807d0a0/initial] [perdir /home/wwwroot/root/] internal redirect with /symfony/web/json.php/user [INTERNAL REDIRECT]
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] add path info postfix: /home/wwwroot/root/symfony/web/json.php -> /home/wwwroot/root/symfony/web/json.php/user
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] strip per-dir prefix: /home/wwwroot/root/symfony/web/json.php/user -> json.php/user
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] applying pattern '.*' to uri 'json.php/user'
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] add path info postfix: /home/wwwroot/root/symfony/web/json.php -> /home/wwwroot/root/symfony/web/json.php/user
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] strip per-dir prefix: /home/wwwroot/root/symfony/web/json.php/user -> json.php/user
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] applying pattern '^$' to uri 'json.php/user'
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] add path info postfix: /home/wwwroot/root/symfony/web/json.php -> /home/wwwroot/root/symfony/web/json.php/user
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] strip per-dir prefix: /home/wwwroot/root/symfony/web/json.php/user -> json.php/user
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] applying pattern '^([^.]+)$' to uri 'json.php/user'
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] add path info postfix: /home/wwwroot/root/symfony/web/json.php -> /home/wwwroot/root/symfony/web/json.php/user
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] strip per-dir prefix: /home/wwwroot/root/symfony/web/json.php/user -> json.php/user
[rewrite:trace3] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] applying pattern '^(.*)$' to uri 'json.php/user'
[rewrite:trace1] [rid#7f8c7805c4a8/initial/redir#1] [perdir /home/wwwroot/root/symfony/web/] pass through /home/wwwroot/root/symfony/web/json.php
[rewrite:trace3] [rid#7f8c780830a0/subreq] [perdir /home/wwwroot/root/] strip per-dir prefix: /home/wwwroot/root/user -> user
[rewrite:trace3] [rid#7f8c780830a0/subreq] [perdir /home/wwwroot/root/] applying pattern '^works$' to uri 'user'
[rewrite:trace3] [rid#7f8c780830a0/subreq] [perdir /home/wwwroot/root/] strip per-dir prefix: /home/wwwroot/root/user -> user
[rewrite:trace3] [rid#7f8c780830a0/subreq] [perdir /home/wwwroot/root/] applying pattern '^fails$' to uri 'user'
[rewrite:trace3] [rid#7f8c780830a0/subreq] [perdir /home/wwwroot/root/] strip per-dir prefix: /home/wwwroot/root/user -> user
[rewrite:trace3] [rid#7f8c780830a0/subreq] [perdir /home/wwwroot/root/] applying pattern '^(.*)$' to uri 'user'
[rewrite:trace2] [rid#7f8c780830a0/subreq] [perdir /home/wwwroot/root/] rewrite 'user' -> '/symfony/web/index.php'
[rewrite:trace2] [rid#7f8c780830a0/subreq] [perdir /home/wwwroot/root/] trying to replace prefix /home/wwwroot/root/ with /
[rewrite:trace2] [rid#7f8c780830a0/subreq] [perdir /home/wwwroot/root/] trying to replace context docroot /home/wwwroot/root with context prefix 
[rewrite:trace1] [rid#7f8c780830a0/subreq] [perdir /home/wwwroot/root/] internal redirect with /symfony/web/index.php [INTERNAL REDIRECT]

So it looks like in the root .htaccess, RewriteRule ^fails$ /symfony/web/json.php/user matches and redirects the request to the .htaccess in symfony/web/, which finds to matches but strips the beginning of the url, sending "/user" back to the original root .htaccess.

Source: Symfony1 Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *