0

I am looking for Apache configuration that would allow for the following:

  • Serve sub.domain.com as /%docRoot%/domain.com/sub, and
  • Would be able to do this for every hosted domain and any subdomain (ie. no per-domain vhost configuration)

I would be grateful for any solution, especially if there was no mod_rewrite involved (using mod_vhost_alias).

Note: There are some obvious solutions using mod_vhost_alias, but they either work for domain.com or sub.domain.com, none of them seems to cover both cases.

Have a nice day!

David
  • 1,055
  • 8
  • 23

1 Answers1

1

Point *.domain.com to your document root (/%docRoot%/). You'll need to do this in a vhost config. In that same vhost, add this:

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

If you want to avoid pointing www.domain.com to /%docRoot%/domain.com/www, then add a condition to exclude it:

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

EDIT:

I assume I will still have to do this for every hosted domain (as the examples you've posted all refer to "domain.com"). Am I right?

Yes, the above will only do the routing for domain.com, if you want to do this for all arbitrary domain.com, you'll need to do something a bit more tricky:

RewriteEngine On
# here, %1 = subdomain name, and %2 = domain name
RewriteCond %{HTTP_HOST} ^([^\.]+)\.(.+)$ [NC]
# make sure the request doesn't already start with the domain name/subdomain name
RewriteCond %{REQUEST_URI}:%2/%1 !^/([^/]+/[^/]+)[^:]*:\1
# rewrite
RewriteRule ^/(.*)$ /%2/%1/$1 [L]

The tricky thing here is the %{REQUEST_URI}:%2/%1 !^/([^/]+/[^/]+)[^:]*:\1 match. It posits the condition: %{REQUEST_URI}:domain.com/sub and makes sure the %{REQUEST_URI} doesn't start with the domain.com/sub (back refrenced from previous match using %2/%1) using the \1 back reference.

With this, you setup your vhost to accept every domain (default vhost) and any subdomain/domain will get routed. Examples:

  • http://blah.bleh.org/file.txt goes to /%docRoot%/bleh.org/blah/file.txt
  • http://foo.bar.com/some/path/ goes to /%docRoot%/bar.com/foo/some/path/
  • http://sub2.sub1.d.com/index.html goes to /%docRoot%/sub1.d.com/sub2/index.html

EDIT2:

Yes, I would very much like to get domain.com routed to /%docRoot%/domain.com/

Try these:

RewriteCond %{HTTP_HOST} ^(.+?)\.([^\.]+\.[^\.]+)$ [NC]
RewriteCond %{REQUEST_URI}:%2/%1 !^/([^/]+/[^/]+)[^:]*:\1
RewriteRule ^/?(.*)$ /%2/%1/$1 [L]

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

Basically the same thing, except a few of the regex needs to be tweaked to separate what a domain.com is like vs sub.domain.com. If you want to redirect www.domain.com to domain.com, that needs to happen before these rules.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • I assume I will still have to do this for every hosted domain (as the examples you've posted all refer to "domain.com"). Am I right? – David Aug 08 '12 at 19:15
  • @David If you want it for any arbitrary "domain.com" as well, it'll get a little more tricky, see edit – Jon Lin Aug 08 '12 at 19:27
  • Jon, I will try to understand the solution, see if it works for me and post you back. Thank you for the help so far! – David Aug 08 '12 at 19:29
  • First, everything up to the first dot (.) will be accessible as `%1` and `%2` will stand for the rest. Second, in the "tricky bit", I make sure *not to route what has already been routed* (to avoid infinite loops). Weird! It works without it as well. Third, I rewrite. Little problem here, though: domain.com will route to `%docRoot%/com/domain`. Jon, would you patiently explain to me why the config works w/o the mentioned part as well? And, would you please head me in the right direction with the "non-subdomain" rewrite? – David Aug 08 '12 at 20:06
  • @David No idea how removing that still works for you. In a blank htaccess file on a test apache install, it internally loops and I get a 500 error without it. As for the second question, where should a `domain.com` get rewritten to? Just `/%docRoot%/domain.com/`? – Jon Lin Aug 08 '12 at 20:09
  • Huh, strange. Will try to find that out on my own, don't worry about that. Yes, I would very much like to get *domain.com* routed to `/%docRoot%/domain.com/` (I will use .htaccess to do a "domain.com" -> "www.domain.com" redirect where appropriate.). – David Aug 08 '12 at 20:13
  • Jon, **thank you very much** for your help. I have been trying hard to find the solution myself somewhere for hours, you gave it in a minute. Have a nice day! – David Aug 08 '12 at 20:31