Forum Moderators: phranque

Message Too Old, No Replies

Mod-rewrite wildcard subdomains to subdirectory

         

erikcw

10:45 pm on Oct 21, 2004 (gmt 0)

10+ Year Member



Hi All,

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

jdMorgan

10:58 pm on Oct 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

erikcw

12:59 am on Oct 25, 2004 (gmt 0)

10+ Year Member



I tried the code from that thread, but it produces a 500 server error. Any ideas why it might do that?


# 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

erikcw

3:17 am on Oct 25, 2004 (gmt 0)

10+ Year Member



Also, forgot to mention - ideally I would like to put this code into httpd.conf - if that makes any difference...

jdMorgan

3:51 am on Oct 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The most likely cause is a typo. After that, some server configuration problem that stops the code from being processed. Have a look at your server error logs to find out.

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