0

Is there any way to allow multiple virtual hosts on one server (apache2 on ubuntu 16.40) to serve files from a directory that is above or adjacent to the virtual host's DocumentRoot?

On my server I have the following directories where site_one, and site_two are the document roots.
websites/site_one
websites/site_two
websites/shared_files/images
websites/shared_files/js

I would like to point http://site_one.com/images/ to shared_files/images/

I tried adding RewriteRule ^images/(.*)$ ../shared_files/images/$1 to my .htaccess which produced a 400 bad request error. Adding Alias "/images" "../shared_files/images" to sites-enabled/site_one.com.conf resulted in a 403 forbidden error.

I can access files with php using either include('../shared_files/file.php) or simply chdir(../shared_files/) but I would prefer not to load images and other static files that way.

SuprMan
  • 350
  • 4
  • 16
  • You need to add the ubuntu user (owner) of `site_one` to the group of `shared_files`. Allowing it to access that directory. If you are in your hosting dir `var/www` for example, type `ls -l` to see which user and groups own the dirs. Then just check which group owns `shared_files` and add the user of `site_one` to that group. More about that here: https://askubuntu.com/questions/79565/how-to-add-existing-user-to-an-existing-group – icecub Nov 29 '18 at 05:38
  • But if this is about securily handling images for your users, I would suggest you check my answer here: https://stackoverflow.com/questions/38509334/full-secure-image-upload-script/38712921#38712921 – icecub Nov 29 '18 at 05:40
  • @icecub `ls -l` shows all the directories having the same user and group. – SuprMan Nov 29 '18 at 05:58
  • In that case `Alias "/images" "../shared_files/images"` should've worked, unless you're using symlinks, in which case you also need to add `Options +FollowSymlinks`. – icecub Nov 29 '18 at 06:04
  • @icecub I tried adding `Options +FollowSymlinks` but still get 403 error. I am setting this up to reduce and simplify dealing with duplicate files, not for user image security, but incidentally I've also been looking for something like your image upload script so thanks for the link! – SuprMan Nov 29 '18 at 07:23

1 Answers1

0

The solution I found was to replace the relative "../images" path in the virtualhost alias with an absolute path such as: Alias "/images" "/var/www/shared_files/images"

SuprMan
  • 350
  • 4
  • 16
  • I would prefer an .htaccess solution. If anyone comes up with one I would gladly accept that as the correct answer. – SuprMan Nov 29 '18 at 08:03