Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm trying to build a catch-all for any subdomains (not captured by previous rewrite rules) for a certain domain, and serve a website from a subdirectory that resides in the same folder as the .htaccess file. I already have my vhosts.conf to send all unmapped requests to a "playground" folder, where I want to easily create new subdomains by simply adding a subfolder.

So, my structure looks like this:

              |-> /foo
              |-> /bar

The .htacces living inside the /playground folder and /foo and /bar being seperate websites. I want to point to /foo and to /bar.

Here is my .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)$ [NC]
RewriteCond %{REQUEST_URI} !^/%1/(.*)
RewriteRule ^(.*) /%1/$1 [L]

This is supposed to capture the subdomain, add it as a subfolder in RewriteRule, then append after the slash and path information. The second RewriteCond is there to prevent an infinite loop. My idea was that %1 in the second RewriteCond would be able to capture the capture group in the first RewriteCond. But so far I haven't had any success, it's always ending up in a redirect loop. If I would replace %1 in the second RewriteCond with hardcoded 'foo' or 'bar', it works, which leads me to believe that you cannot refer to a capture group inside a RewriteCond. Is is true? Or am I missing something?

share|improve this question
Why not create virtualhosts for that? That is what they are for. – Frands Hansen Oct 22 '12 at 22:37
At a guess, indorock's customers don't have access to vhosts, can't restart Apache and expect to see the subdomain work instantly. Please confirm if this (or something like it) is correct. – Ladadadada Oct 23 '12 at 18:48

1 Answer 1

Hope I understood you correctly:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)$ [NC]
RewriteCond %{REQUEST_URI} !%1
RewriteRule ^(.*)$ /%1/$1 [L]
share|improve this answer

Your Answer


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.