Hailwood
Hailwood

Reputation: 92581

Catch all sub domains?

on my localhost I develop modules for a cms.

Hence I have a lot of urls like so:

cart.cfox.dev  
blog.cfox.dev  
orders.cfox.dev  

which map to

/var/www/cfox/modules/cart
/var/www/cfox/modules/blog
/var/www/cfox/modules/orders

Each time I setup a new module I create a new virtual host.

but they are all the same with one word changed.

What I was wondering is if there is a way to have one virtual host deal with all of these.

The other one is my hosts file is full of

127.0.0.1 cart.cfox.dev
127.0.0.1 blog.cfox.dev
127.0.0.1 orders.cfox.dev

Can we do the same thing there?

Upvotes: 0

Views: 250

Answers (2)

Kamil Šrot
Kamil Šrot

Reputation: 2221

You can do it using mod_rewrite.

Setup your default apache virtual host with DocumentRoot in /var/www and place there following .htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.dev$ [NC]
RewriteCond %{REQUEST_URI} !^/.+/modules/.+/.*$
RewriteRule .* /%2/modules/%1%{REQUEST_URI} [QSA,L]

Sure, you have to make sure, your apache reads .htaccess files (AllowOverride)

Upvotes: 1

julp
julp

Reputation: 4010

I think the best way to handle these virtualhosts is a VirtualDocumentRoot. Specially with Apache 2.4 where, as I know, the DocumentRoot is not wrong anymore.

UseCanonicalName Off
VirtualDocumentRoot /var/www/cfox/modules/%1

<Directory "/var/www/cfox/modules/*">
    # Apache < 2.4
    <IfModule !mod_authz_core.c>
        Allow from all
    </IfModule>
    # Apache >= 2.4
    <IfModule mod_authz_core.c>
        Require all granted
    </IfModule>
</Directory>

Can we do the same thing there?

Unfortunately, no.

Upvotes: 1

Related Questions