Score:2

Apache incorrect url rewrite for non-existent symlink files

ar flag

I have a directory (foo/uploads), which contains 1 file: file1.txt

I have created a symlink to this directory (bar/uploads)

Here is my .htaccess file inside bar:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

And this is my vhost file:

<VirtualHost *:80>
    ServerName bar.com
    DocumentRoot /var/www/bar

    <Directory /var/www/bar>
        allow from all
        order allow,deny
        AllowOverride All
    </Directory>
</VirtualHost>

The problem I'm having is, if I try to access a non-existent file when using the symlinked folder, it is not producing a 404 response. Here is what I mean:

  1. foo.com/uploads/file1.txt -> file served (CORRECT)
  2. foo.com/uploads/file2.txt -> Apache 404 ErrorDocument served (CORRECT)
  3. bar.com/uploads/file1.txt -> file served (CORRECT)
  4. bar.com/uploads/file2.txt -> proceeds to index.php (INCORRECT)

I have also tried including:

RewriteCond %{REQUEST_FILENAME} !-l

Before the RewriteRule, but that doesn't make a difference.

Anyone know what I need to do?

Score:1
kz flag

If you only want the front-controller to apply when accessed via bar.com then you could check the requested Host header in the rule. For example:

RewriteCond %{HTTP_HOST} (^|\.)bar\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Or, exclude the /uploads subdirectory:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^uploads/ index.php [L]

Or, include another .htaccess file in the /uploads subdirectory that overrides the parent front-controller pattern. You can do this simply by disabling (or enabling - providing mod_rewrite inheritance is not enabled) the rewrite engine in the subdirectory:

# /uploads/.htaccess
RewriteEngine Off

Alternatively, disable .htaccess overrides altogether and move these directives to the <Directory /var/www/bar> container inside the bar.com vHost.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.