Welcome to WebmasterWorld Guest from 50.19.53.104

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Mass virtual hosting : How do I do it?

All subdomains matching two letters should point to the same place

     

referer

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

10+ Year Member



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

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

10+ Year Member



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

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

WebmasterWorld Senior Member 10+ Year Member



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>
 

Featured Threads

Hot Threads This Week

Hot Threads This Month