Search code examples
php.htaccesssymfony1

using .htaccess to modify asset paths base on domain (multiple sites on one Symfony app)


I'm building a Symfony application that's made to run many sites. Similar to this article.

So far so good except he uses some .htaccess "voodoo". To make it so image and CSS file links get rewritten so to point to a assets directory named after the current domain. Again read the article above if that doesn't make sense.

Here is the line from the article:

RewriteCond /home/ash/projects/carshop/trunk/web/perhost/%{HTTP_HOST}%{REQUEST_FILENAME} -f
RewriteRule (.*) /perhost/%{HTTP_HOST}/$1 [L] 

Here is the same line adapted for my Windows testing server:

RewriteCond C:/xampp/projects/listing/web/sites/%{HTTP_HOST}%{REQUEST_FILENAME} -f
RewriteRule (.*) /sites/%{HTTP_HOST}/$1 [L]

Is there something wrong with my syntax or am I not understanding what this .htaccess code is supposed to do. I've checked and mod_rewrite is on.

What I'm trying to do is make it so I don't have to query the database (to retrieve the current website) in order to get the correct path to the files. Each domain would have its images, CSS, files in its own directory which is named after itself.

like this

  • web/sites/website1.com/{images,js,css}
  • web/sites/website2.com/{images,js,css}

Solution

  • REQUEST_FILENAME already is an absolute filesystem path:

    REQUEST_FILENAME
    The full local filesystem path to the file or script matching the request.

    But why don’t you use $1 in the condition too as that’s also the destination you want to redirect to:

    RewriteCond /home/ash/projects/carshop/trunk/web/perhost/%{HTTP_HOST}/$1 -f
    RewriteRule (.*) /perhost/%{HTTP_HOST}/$1 [L]