3

I've got a wildcard DNS record, allowing all forms of *.domain.com, additionally I'm pointing several different domains at the same machine and using rewrite rules to direct requests to sub folders depending upon the url the request originates.

For instance domain1.com point to /sites/folder/domain1, domain2.com points to /sites/folder/domain2

My problem is, that i'm using the Apache var HTTP_HOST in this rule, which includes the sub domain so that sub1.domain1.com points to /sites/folder/sub1.domain1.com and with several hundred sub domains for each domain there is no way to create all those folder possibility.

My question: how to remove all sub-domains from the HTTP_HOST variable? This is what the ruleset looks like thus far:

RewriteRule ^file.xml$ sites/%{HTTP_HOST}/file.xml [L]

Which works, until a sub domain is included in the URL...

Jackson
  • 31
  • 3

2 Answers2

1

To limit your rule to just mydomain.com (ie no subdomains) add a RewriteCond directive before the Rewrite as below

RewriteCond %{HTTP_HOST} ^[^\.]+\.com$ [NC]
RewriteRule ^file.xml$ sites/%{HTTP_HOST}/file.xml [L]

Edit: Allow any domains, but no subdomains

Ulrich Palha
  • 9,411
  • 3
  • 25
  • 31
  • 1
    This would work very well if I wasn't needing to do this to a large number of domains, all of which point to the same machine. In this example, mydomain.com could be any one of several different domains. – Jackson Dec 20 '11 at 20:03
  • Above should work for anydomain.com, where anydomain can be anything – Ulrich Palha Dec 20 '11 at 20:11
  • this does look like it would work, but I'm not able to get the RewriteCond to have any effect. It is still including the sub domain in the path and not finding the files. Maybe I need to look at the apache config... – Jackson Dec 20 '11 at 21:18
0

I think what he wants is to use the variable HTTP_HOST in a rewrite, but NOT with a subdomain:
HTTP_HOST = www.example.com
Rewrite rule:
RewriteRule ^file.xml$ sites/%{HTTP_HOST}/file.xml [L]
is, in actuality:
RewriteRule ^file.xml$ sites/example.com/file.xml [L]
but NOT:
RewriteRule ^file.xml$ sites/www.example.com/file.xml [L]
-Brian