-1

I have a catch-all Apache virtual host in /var/www/. In it, I have a index.php that handles all the code for pages, and subfolders holding the resources for each site (images, css, etc).

I want a rewrite that uses the images for the appropriate domain in their subfolder if they exists, and hands off to index.php if they don't. I also don't want to be able to access these subfolders directly.

Example

http://example.com/images/fb.png -> /var/www/sites/example.com/images/fb.png http://example.com/sites/example.com/images/fb.png -> /var/www/index.php (to show 404) http://example.com/whatever -> /var/www/index.php

It seems simple, but I can't figure out how to block the /sites/.

.htaccess

This works with the exception that it allows http://example.com/sites/example.com/images/fb.png to be accessed directly.

RewriteCond %{REQUEST_URI} !^/sites/
RewriteRule ^(.*)$ sites/%{HTTP_HOST}/$1 [NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php

Trace

[rewrite:trace3] [/initial] [perdir /var/www/] add path info postfix: /var/www/images -> /var/www/images/fb.png
[rewrite:trace3] [/initial] [perdir /var/www/] strip per-dir prefix: /var/www/images/fb.png -> images/fb.png
[rewrite:trace3] [/initial] [perdir /var/www/] applying pattern '^(.*)$' to uri 'images/fb.png'
[rewrite:trace2] [/initial] [perdir /var/www/] rewrite 'images/fb.png' -> 'sites/example.com/images/fb.png'
[rewrite:trace3] [/initial] [perdir /var/www/] add per-dir prefix: sites/example.com/images/fb.png -> /var/www/sites/example.com/images/fb.png
[rewrite:trace3] [/initial] [perdir /var/www/] add path info postfix: /var/www/sites/example.com/images/fb.png -> /var/www/sites/example.com/images/fb.png/fb.png
[rewrite:trace3] [/initial] [perdir /var/www/] strip per-dir prefix: /var/www/sites/example.com/images/fb.png/fb.png -> sites/example.com/images/fb.png/fb.png
[rewrite:trace3] [/initial] [perdir /var/www/] applying pattern '.' to uri 'sites/example.com/images/fb.png/fb.png'
[rewrite:trace2] [/initial] [perdir /var/www/] strip document_root prefix: /var/www/sites/example.com/images/fb.png -> /sites/example.com/images/fb.png
[rewrite:trace1] [/initial] [perdir /var/www/] internal redirect with /sites/example.com/images/fb.png [INTERNAL REDIRECT]
[rewrite:trace3] [/initial/redir#1] [perdir /var/www/] strip per-dir prefix: /var/www/sites/example.com/images/fb.png -> sites/example.com/images/fb.png
[rewrite:trace3] [/initial/redir#1] [perdir /var/www/] applying pattern '^(.*)$' to uri 'sites/example.com/images/fb.png'
[rewrite:trace3] [/initial/redir#1] [perdir /var/www/] strip per-dir prefix: /var/www/sites/example.com/images/fb.png -> sites/example.com/images/fb.png
[rewrite:trace3] [/initial/redir#1] [perdir /var/www/] applying pattern '.' to uri 'sites/example.com/images/fb.png'
[rewrite:trace1] [/initial/redir#1] [perdir /var/www/] pass through /var/www/sites/example.com/images/fb.png

I don't get the mod-rewrite and it's looping structure... I would love to get some help with this. Mod-rewrite always seems to give me trouble.

Sarke
  • 371
  • 1
  • 3
  • 12
  • Not even close to a duplicate. That link is a great summary of common scenarios, but they don't help me. – Sarke Oct 07 '14 at 06:37
  • It is a **canonical** article. Those are articles where the community has said all it is going to say on a class of subject, because although everyone's particular problems in that class are somewhat different, to the extent that they are interesting, they aren't different, and to the extent that they're different, they aren't interesting (to anyone save the questioner). So we write one answer that's designed to be the last word on the subject, and say no more. – MadHatter Oct 24 '14 at 19:30

1 Answers1

0

This does what I want.

# rewrite to site subfolder
RewriteRule ^(.*)$ sites/%{HTTP_HOST}/$1 [NC,QSD,DPI]

# if file doesn't exists in subfolder, just pass it to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [END]

# prevent another go-around
RewriteRule .* - [END]
Sarke
  • 371
  • 1
  • 3
  • 12