Dynamic vhosts and combined directories (Apache 2.2)

I have an old account on with a provider that gives me the opportunity to redirect non-www requests to their www counterparts (without doing it with .htaccess files). I want to mimic that option.

Lets take "domain.com" as an example.

(This is what I want to happen, not how it works right now)

domain.com redirects to www.domain.com but reads from /domain.com (same as domain.com would without the re-direct).

subdomain.domain.com reads from /subdomain.domain.com

I don't want to set up VirtualHosts for every single domain/subdomain, but I'm starting to think this is the only way. A lot of examples uses VirtualDocumentRoot /%0- and that would work for the subdomains but not domain.com as it would result in apache looking for /www.domain.com

Is what I'm trying to do possible or am I just trying to be much too dynamic for no good reason?

4 Replies

````
ServerName domain.com
ServerAlias www.domain.com

Other stuff in here as required

````

Should get the first bit happening for you.

Looks like the direction I was heading myself, this is how I've currently set it up, any sugestions are welcome.

../sites-available/default looks like this:

DocumentRoot /sites
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

 <directory sites="">Options ExecCGI -Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
        AddHandler fcgid-script .php
        FCGIWrapper /usr/lib/cgi-bin/php5 .php</directory> 

NameVirtualHost *:80
 <virtualhost *:80="">DocumentRoot /sites/default/public_html</virtualhost> 

And then for each domain I do this:

 <virtualhost *:80="">ServerName domain.com
        ServerAlias www.domain.com

        RewriteEngine On
        RewriteCond %{HTTP_HOST} ^domain\.com$
        RewriteRule (.*) http://www.domain.com [R=301,L]

        DocumentRoot /sites/domain.com/public_html</virtualhost> 
 <virtualhost *:80="">ServerName domain.com
        ServerAlias *.domain.com
        VirtualDocumentRoot /sites/%0/public_html</virtualhost> 

http://gentoo-wiki.com/ApacheModulesmodvhostalias

Could probably be applied to other distributions

@OverlordQ:

http://gentoo-wiki.com/ApacheModulesmodvhostalias

Could probably be applied to other distributions
Thanks for the link, the problem with that one is that example.com and www.example.com gets their own directories.

Too bad you can't do if (%-3.0 == "www") { $dir == "non-www-dir")

;)

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct