request matching RewriteRule returns permission denied

Viewed 44

I am experiencing a bizarre apache error. I'd like to rewrite all requests from /api/media/fi/le/path.jpg to /media/fi/le/path.jpg

This are rewrite rules setup in virtual host:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /api/v1/* /src/api/v1/index.php [L]
RewriteRule /api/media/(.*) /media/$1 [L]

If I point the browser to /media/fi/le/path.jpg, the image gets server successfully. If I go to /api/media/fi/le/path.jpg it complains that I do not have permission to access the resource. I must have permission, because I can access the file if no rewrites are done. How should I rewrite the last RewriteRule so that it would serve the images?

I added logging directive LogLevel alert rewrite:trace6 to see what the apache is doing under the covers. This are the results:

 init rewrite engine with requested uri /api/media/32/ee/0e60731bcb220c7c7b.jpg
 applying pattern '/api/v1/*' to uri '/api/media/32/ee/0e60731bcb220c7c7b.jpg'
 applying pattern '/api/media/(.*)' to uri '/api/media/32/ee/0e60731bcb220c7c7b.jpg'
 rewrite '/api/media/32/ee/0e60731bcb220c7c7b.jpg' -> '/media/32/ee/0e60731bcb220c7c7b.jpg'
 local path result: /media/32/ee/0e60731bcb220c7c7b.jpg
 go-ahead with /media/32/ee/0e60731bcb220c7c7b.jpg [OK]

I can visit the url mydomain.com/media/32/ee/0e60731bcb220c7c7b.jpg manually and the image gets server with no problem. What am I missing?

Complete .conf file for virtual host:

<IfModule mod_ssl.c>
      <VirtualHost *:443>
            DocumentRoot /home/user/project/src
            ServerName project.com
            ErrorLog logs/project-error_log
            CustomLog logs/project-access_log common
            DirectoryIndex index.php
    
            <Directory /home/user/project/src>
                Options -Indexes +FollowSymLinks
                AllowOverride None
                Require all granted
            </Directory>
    
            LogLevel alert rewrite:trace6
            RewriteEngine on
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule /api/v1/* /api/v1/index.php
            RewriteRule /api/media/(.*) /media/$1 [L]
    
            SSLCertificateFile /etc/letsencrypt/live/project.com/cert.pem
            SSLCertificateKeyFile /etc/letsencrypt/live/project.com/privkey.pem
            Include /etc/letsencrypt/options-ssl-apache.conf
            SSLCertificateChainFile /etc/letsencrypt/live/project.com/chain.pem
        </VirtualHost>
    </IfModule>
1 Answers

There is weird behaviour with rewrite engine.

RewriteRule /api/media/(.*) /media/$1 [L]

Will always return forbidden. After mv media images and changing the above directive to RewriteRule /api/media/(.*) /images/$1 [L], permission error is gone.

Related