Issue
I want to create a setup in apache where my media files are split from my code (for easy project management). The idea I created is to have a root directory (/home/villermen/httpd/root) which contains all the code (php files, css files etc.) and a media directory (/home/villermen/httpd/media) which contains all non-text files.
Executing this setup has proven to be tricky. I'm trying to use mod_rewrite to serve files from the media directory if they exist, but I haven't been successful at all so far.
Here's the part of my httpd.conf in which I try to make the magic happen:
DocumentRoot /home/villermen/httpd/root
RewriteEngine on
<Directory /home/villermen/httpd/root>
Order allow,deny
Allow from all
AllowOverride All
#...other magic magoo, including rewriterules that do work
</Directory>
<Directory /home/villermen/httpd/media>
Order allow,deny
Allow from allow
AllowOverride All
</Directory>
#Serve from media if file exists there
RewriteCond "/home/villermen/httpd/media%{REQUEST_URI}" -f
RewriteRule "^/?(.*)$" "/home/villermen/httpd/media/$1"
Trying to access a file which exists in the media folder will still throw a 404, I'm at a loss here. Is there anything I'm not seeing?
I'm using apache 2.4 on ubuntu 14.04.
EDIT: Removing the RewriteCond altogether will still not make any magic happen. mod_rewrite should work outside of Directory tags right?
UPDATE: See @kannan-mohan's possible answer below. In the end I went for the approach of linking the media folder from the webroot to it's folder and then rewriting everything to there:
DocumentRoot /home/villermen/httpd/root
Alias /media /home/villermen/httpd/media
<Directory /home/villermen/httpd/root>
#Serving files from media if they exists there
RewriteCond "/home/villermen/httpd/media%{REQUEST_URI}" -f
RewriteRule "^(.*)$" "/media/$1"
</Directory>
<Directory /home/villermen/httpd/media>
Order allow,deny
allow from all
AllowOverride All
</Directory>
Solution
If my understanding is correct what you need is as below.
http://example.com/media/hello.jpg
should map to/home/villermen/httpd/media/hello.jpg
http://example.com/media/video/world.mp4
should map to/home/villermen/httpd/media/video/world.mp4
- And in case if the file does not exist in
media
directory then it shouldthrough HTTP 404fetch the image fromroot
directory. And sohttp://example.com/media/video/world.mp4
should map to/home/villermen/httpd/root/video/world.mp4
In that case below configuration will be enough.
<VirtualHost *:80>
ServerName example.com
DocumentRoot /home/villermen/httpd/root
RewriteEngine on
RewriteCond "%{DOCUMENT_ROOT}..%{REQUEST_URI}" -f
RewriteCond %{REQUEST_URI} .*\.jpg$ [OR]
RewriteCond %{REQUEST_URI} .*\.mp4$
RewriteRule (.*) "%{DOCUMENT_ROOT}..$1" [L]
RewriteRule /media/(.*) /$1 [L]
</VirtualHost>
For added safty the RewriteCond
will also check if the file requested is of type jpg
or mp4
and only then the rewrite happens. More media types can be added by appending [OR]
flag at end of each condition.
Answered By - Kannan Mohan