apache - htaccess to redirect to a sub-directory based on the host name -


question:

what ruleset need 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] , [4] requested file exist in original path, no redirect takes place.

background:

i have wamp envo uses apache friend's excellent xampp. work on multiple projects different clients , have set following directory structure:

htdocs   + css   + images   + includes   + js   + projects      + bigcommerce      + xyzblog      ... 

when start working on new project create directory same name under projects; moreover, name project after host name of client. if start working on project production site http://www.dreamvacation.com add dreamvacation directory under projects directory.

lastly temporarily, add following line hosts file in c:\windows\system32\drivers\etc

127.0.0.1   dreamvacation.com 

that way requests dreamvacation.com short circuited localhost.

i still need 1 component able code on localhost , expect behave same way when uploaded production server. htaccess file check host name in request , redirect directory same name less top domain identifier (.com, .net, etc). redirection should take place if requested file not exist. last condition there because place used files in directories under htdocs - instance have 1 jquery-min.js in htdocs/js , 1 blueprint css in htdocs/css/blueprint/screen.css. each project has js, css, ... subdirectory project-specific files.

how achieved?

you should have @ named based virtualhosts. starting directory root on each client directory. real solution :-)

for shared folers use "alias" instructions in these virtualhost directories target same places on directory tree. , use links share shared folders.

anyway can on own way well. used mass virtual hosting, writing thousands of virtualhosts pain, mode_rewrite replacement solution.

to detect exisiting files , directories can use rewritecond directives , prevent rewriting if file exists. on real rewrite need reuse apache variable %{http_host}. so, did not test should starting point (user rewriteloglevel , rewritelog debug),:

rewriteengine on rewritebase / rewritecond   %{request_filename} !-f rewritecond   %{request_filename} !-d rewritecond   %{http_host}                 ^www\.[^.]+\.com$ rewriterule   ^(.+)                        %{http_host}$1          [c] rewriterule   ^www\.([^.]+)\.com(.*) /htdocs/projects/$1$2 

this based on apache documentation examples username based domains, should check this apache doc page "mass virtual hosting" more details, of example includes file-based rules , checks not need on dev env. remember using virtualhosts without mod_rewrite easier mod_rewrite can quite if want.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -