homepage Welcome to WebmasterWorld Guest from 54.204.94.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Mass virtual hosting : How do I do it?
All subdomains matching two letters should point to the same place
referer




msg:1504123
 8:28 am on Jun 27, 2005 (gmt 0)

How can I change the httpd.conf so that I don't have to write every subdomain?

I would like everything matching /^\w\w\.widgets\.com$/i go to the same DocumentRoot. Is it possible?

Current settings:


<VirtualHost *:80>
ServerAdmin webmaster@widgets.com
ServerName www.widgets.com no.widgets.com fi.widgets.com dk.widgets.com ..... ru.widgets.com
DocumentRoot /opt/widgets/public_html
</VirtualHost>

 

gergoe




msg:1504124
 4:33 pm on Jun 27, 2005 (gmt 0)

The first thing you need to do is to make a VirtualHost which processes all the incomming requests - except the ones which has a corresponding VirtualHost defined. This is quite easy, you only need to put the VirtualHost definition on the top of the others, so it should become the first VirtualHost in your config. By doing so it becomes the default VirtualHost, which will handle all requests not matching any other VirtualHost.
So considering the following (rough) example:

<VirtualHost *>
ServerName example1.com
DocumentRoot /example1/rest
</VirtualHost>
#
<VirtualHost *>
ServerName aa.example1.com
DocumentRoot /example1/aa
</VirtualHost>
#
<VirtualHost *>
ServerName www.example1.com
DocumentRoot /example1/
</VirtualHost>

The requests would have been served by the following VirtualHosts:
www.example1.com -> 3rd
aa.example1.com -> 2nd
ru.example1.com -> 1st
qw.example1.com -> 1st
BUT!
wqerty.example1.com -> 1st
dummy.com -> 1st

So the 1st VirtualHost processes everything which is not matched by the other VirtualHosts. The next step is to make sure that it only processes the requests you need, the ones for the xx.example1.com hosts. Practically we do the opposite, we send away requests which should not processed by that VirtualHost.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^([a-z]{2})\.example1\.com(:[0-9]+)?$ [NC]
RewriteRule ^/ h**p://www.example1.com/nosuchhost.html [R=301,L]

This piece of rewrite rule will redirect all requests to the h**p://www.example1.com/nosuchhost.html url if it should not be server by this VirtualHost. Note the (:[0-9]+)? at the end of the RewriteCond; this ensures that if a browser or a spider sends the port number (usually 80) in the hostname with the request, then it won't break up the rewriting. Addtionally if you don't want the redirection when there are no such VirtualHost to handle the request, then you can change the last RewriteRule like this:

RewriteRule ^/ - [G,L]

By doing so, instead of a redirection, a standard HTTP error message will be displayed stating that the requested resource can not be found.

So the final result is something like this:
www.example1.com -> 3rd
aa.example1.com -> 2nd
ru.example1.com -> 1st
qw.example1.com -> 1st
wqerty.example1.com -> Redirected to the 3rd
dummy.com -> Redirected to the 3rd

Have fun

moltar




msg:1504125
 4:49 pm on Jun 27, 2005 (gmt 0)

Haven't tested this, but try it:

<VirtualHost *:80>
ServerAdmin webmaster@widgets.com
ServerName www.widgets.com
ServerAlias *.widgets.com
DocumentRoot /opt/widgets/public_html
</VirtualHost>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved