2

I am looking for a solution to create an alias for a varying path on the server determined by the subdomain name. Where the folder is behind the document root.

How can I make http://*.site.com/images/ point to /var/www/site/clients/*/images/?

Something like:

Alias /images "/var/www/site/clients/$(var)/images/"

(Please note I am not looking into changing the document root to /var/www/site/clients/*/.)

<VirtualHost *.domain.tld:80>
  ServerName *.domain.tld
  DocumentRoot "/var/www/site/public_html"

  <Directory "/var/www/site/public_html">
    Options -Indexes
    DirectoryIndex index.php index.html
    AllowOverride All
  </Directory>

  Alias /images "/var/www/site/clients/*/images"
</VirtualHost>

Attempt #1 - Fails with 404:

RewriteCond %{HTTP_HOST} ^([^.]+)\.
RewriteRule ^/images/(.*)$ /var/www/site/clients/%1/images/$1 [L]

Attempt #2 - Fails with 404:

SetEnvIf Host "^([^.]+)\." SUBDOMAIN=$1
Aliash /images /var/www/site/clients/%{env:SUBDOMAIN}/images
tim
  • 2,530
  • 3
  • 26
  • 45

1 Answers1

0

I came up with this, which has some dynamics and runs on .htaccess-level:

<IfModule mod_rewrite.c>
  RewriteEngine On

  # Web path to application root
  RewriteBase /

  # Set environment variables, also fetchable in PHP e.g. $_SERVER['CLIENT']
  SetEnvIf Host "^(.*)\.domain\.tld$" CLIENT=$1 APP_VERSION=1.0.0

  # Map some storage content to root, based on environment variable
  RewriteRule ^(storage/.*)$ clients/%{CLIENT}/$1 [QSA,L]

  # Forbid access to the other directories
  RewriteCond %{ENV:REDIRECT_STATUS} ^$
  RewriteRule ^clients/ - [F,R=404,L]

  # No rewrite logic for physical files
  RewriteCond %{REQUEST_FILENAME} -d [OR]
  RewriteCond %{REQUEST_FILENAME} -f [OR]
  RewriteCond %{REQUEST_FILENAME} -l
  RewriteRule ^ - [L]

  # Default route
  RewriteRule ^ index.php [QSA,L]
</IfModule>
tim
  • 2,530
  • 3
  • 26
  • 45