Question:
What ruleset do I need to do the following redirects:
[1] http://www.xyzblog.com/foo/bar.php => htdocs/projects/xyzblog/foo/bar.php
[2] http://www.xyzblog.com/js/jquery-min.js => htdocs/js/jquery-min.js
[3] http://www.bigcommerce.ch/images/baz.png => htdocs/projects/bigcommerce/images/baz.png
[4] http://www.bigcommerce.ch/css/reset.css => htdocs/css/reset.css
In [2] and [4] the requested file does exist in the original path, so no redirect takes place.
Background:
I have a WAMP envo which uses Apache Friend’s excellent XAMPP. I work on multiple projects for different clients and have set up the following directory structure:
htdocs
+ css
+ images
+ includes
+ js
+ projects
+ bigcommerce
+ xyzblog
...
When I start working on a new project I create a directory with the same name under projects; moreover, I name the project after the host name of the client. So if I start working on a project where the production site would be http://www.dreamvacation.com I add a dreamvacation directory under the projects directory.
Lastly I temporarily, add the following line to my hosts file in C:\WINDOWS\system32\drivers\etc
127.0.0.1 dreamvacation.com
That way all requests to dreamvacation.com would be short circuited to my localhost.
I still need one component to be able to code on localhost and expect it to behave the same way when uploaded to the production server. That is an htaccess file to check what the host name is in the request and redirect to a directory with the same name less the top domain identifier (.com, .net, etc). The redirection should only take place if the requested file does not exist. This last condition is there because I place frequently used files in directories under htdocs – for instance I only have one jquery-min.js in htdocs/js and only one blueprint css in htdocs/css/blueprint/screen.css. However each project also has a js, css, … subdirectory but only for project-specific files.
How could this be achieved?
You should certainly have a look at named based virtualhosts. Starting the directory root on each client directory. This is the real solution 🙂
For your shared folers you could use “Alias” instructions in these Virtualhost so that some directories would always target the same places on the directory tree.
and use links to share your shared folders.
Anyway you can do on your own way as well. It something used for Mass Virtual Hosting, where writing thousands of Virtualhosts is a pain, mode_rewrite is a good replacement solution.
To detect exisiting files and directories you can use the RewriteCond directives and prevent the rewriting if the file exists.
Then on the real rewrite you need to reuse the apache variable
%{HTTP_HOST}. So, I did not test it but something like that should be a good starting point (user RewriteLogLevel and RewriteLog to debug),:This is based on apache documentation examples for username based domains, you should check this apache doc page for “Mass Virtual Hosting” for more details, but some of the example includes file-based rules and checks that you do not need on your dev env. Remember using VirtualHosts without mod_rewrite is certainly easier but mod_rewrite can do quite everything if you want.