Reputation: 21
I'm creating a series of third level domain with php using Virtualmin API (and till here all's right). I need a solution to serve to each of these subdomains the corresponding subdirectory, that is
sub1.domain.com goes in C:\xampp\htdocs\domain_com\subsites\sub1;
sub2.domain.com => C:\xampp\htdocs\domain_com\subsites\sub2.
I should do this with Virtualmin API, too, but then the user can see the subdirectory in which he is redirected, and it is bad. Subdomain should be converted in subdirectories with a general solution in httpd.conf to not modifying and restarting Apache continuosly. In the meanwhile the URL showed to the user must remain "sub1.domain.com/whatever_dir/user_file.php".
I've tried to obtain it with RewriteRule like these ones:
<VirtualHost *:80>
ServerName www.domain.com
ServeAlias *.domain.com
DocumentRoot C:/xampp/htdoc/domain_com/
Option +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.com
RewriteCond %{HTTP_HOST} ^(w*).domain.com
RewriteRule ^$ subsites/%1%{REQUEST_URI}
</VirtualHost>
With this rule i succeded to call all files in '%1' directory. But if anyone of these files calls a picture or any other resource the path of this resource refers always to the document root, not to the calling file. So the file is correctly called but with wrong resource paths!!!
And it makes so with Alias, AliasMatch, too. I begin to realize that Alias and RewriteRules are not the right means for this goal but i don't know any other ones.
Thanks for attention. I hope someone have a suggestion. Emanuele.
Upvotes: 2
Views: 2120
Reputation: 21
J've casually fixed myself the problem. A RewriteRule has the effect to redirect the request elsewhere. Each time it does this Apache recall itself for a new appliance of it, and this caused the internal server error! – Excuse me, i don't know how to use this blog. Here is the fix.
<VirtualHost :80>
ServerName www.domain.it
ServerAlias *.domain.it
DocumentRoot "C:/xampp/htdocs/domain_com"
<Directory "C:/xampp/htdocs/domain_com">
Options FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^([0-9A-Za-z_-]+).domain.com(.*/?)([0-9A-Za-z-_.?&=%])$
RewriteRule !^siti siti/%1/%2%3
</Directory>
In this way firstly the rule adds the prefix "siti/" to the request, and whatother needs to me. When Apache turn again on the path, it matches the new one against not "!", beginning "^" with "siti", and stops it. In fact it functions perfectly.
Upvotes: 0
Reputation: 573
Kevin,
You should look at http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html to do this for you. You will not have to perform any future changes to the configuration as long as the directory structure stays the same.
Upvotes: 1