Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to set up multiple domains on my Amazon EC2 micro instance running Ubuntu Server 12.04. I installed Apache correctly and set up virtual hosts but having problems with wildcard subdomains.

This is what my httpd.conf file looks like

NameVirtualHost *:80

<VirtualHost *:80>
    UseCanonicalName Off
    VirtualDocumentRoot /home/username/domains/%0/html/
</VirtualHost>

My DNS records (on Amazon Route 53) are:

domain.tld      A    1.2.3.4
*.domain.tld    A    1.2.3.4

If i create a test.domain.tld directory with the html subdirectory, it works fine. But what I want to do is to redirect *.domain.tld to domain.tld in case there is no directory for the sub-domain accessed. I would also like www.domain.tld to redirect to domain.tld. The system should also work if I decide to host another website, example.com, on the server.

I tried Googling a lot but without any luck. Suggestions?

share|improve this question
    
This is all apache, sites-available, and .htaccess related. Have you considered using nginx? Some people say it is easier to set up. –  Chris K Jun 17 '12 at 20:39
    
No but I want to use Apache. It's used by practically every web hosting company out there so there has to be some way to do this –  Ashish Kumar Jun 17 '12 at 21:27
    
Yes, and people still have telephones with push bottons that plug into a wall! Your question is google-able and just takes some work on a webserver. I have set up 3 or 4 apache servers; trial and error answered my problems like this. Oh, read this: ichilton.co.uk/blog/web/… –  Chris K Jun 17 '12 at 21:29
    
I have a rotary phone sitting on my desk, too. It doesn't dial out on my VoIP line. Such is the nature of progress. Web hosting companies only use Apache because it's what their billing and provisioning software supports (though some are starting to switch to nginx). –  Michael Hampton Jul 21 '12 at 1:13
add comment

1 Answer

You can try using mod_rewrite to check whether the document root exists, and send a redirect if it doesn't.

RewriteCond %{DOCUMENT_ROOT} ! -d
RewriteRule .* http://domain.tld/ [R]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.