Forum Moderators: phranque
I am trying to get wildcard DNS setup on my server. So far, the DNS part works great, except for the fact that anything.domain.com => www.domain.com
I want to set it up so that
anything(*).domain.com => www.domain.com/anything
This is what I have so far:
RewriteCond %{HTTP_HOST} jane.domain.com$
RewriteCond %{REQUEST_URI}!jane/
RewriteRule ^(.*)$ jane/$1
How do I get the RewriteCond to work in a wildcard fashion:
Something like:
RewriteCond %{HTTP_HOST} (.*).domain.com$
RewriteCond %{REQUEST_URI}!(.*)/
RewriteRule ^(.*)$ $1/$2
Thanks!
Erik
See this thread on how to rewrite arbitrary subdomains to subdirectories [webmasterworld.com] (message 6). The code is complex because you need to avoid an infinite rewrite loop when a page in the subdomain/subdirectory is requested.
Jim
# Rewrite <subdomain>.example.com/<path> to example.com/<subdomain>/<path>
#
# Skip rewrite if no hostname or if subdomain is www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\. [NC]
# Extract (required) subdomain (%1), and first path element (%3), discard port number if present (%2)
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.dreamprogress\.com(:80)?<>/([^/]*) [NC]
# Rewrite only when subdomain not equal to first path element (prevents mod_rewrite recursion)
RewriteCond %1<>%3!^(.*)<>\1$ [NC]
# Rewrite to /subdomain/path
RewriteRule ^(.*) /%1/$1 [L]
Thanks!
Erik
A minor change is required to make the code work in httpd.conf: add a leading slash to the URL-patterns in the RewriteRules. So
RewriteRule ^(.*) /%1/$1 [L]
becomes
RewriteRule ^/(.*) /%1/$1 [L]
Jim