4

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:

/var/www/playground
              |-> /foo
              |-> /bar

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

Here is my .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+).domain.com$ [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?

  • 2
    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
0

Hope I understood you correctly:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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