Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

automatically routing subdomains via mod_rewrite?

instead of hand-coding each rule for each sub

4:54 am on Jan 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

I've read a few threads on here about using mod_rewrite
for pseudo subdomains but I am looking to produce a superior
routine that doesn't require hand coding of each sub into .HTACCESS
Need some help from the mod_rewrite Gurus!

I've gotten my host to turn on wildcard DNS for me and the follow code definitely works properly:

RewriteCond %{HTTP_HOST} images\.mydomain\.com [NC]
RewriteCond %{REQUEST_URI}!^/images/.*
RewriteRule (.*)/images/$1[L]

So if [images.mydomain.com...] is entered,
it transparently uses [mydomain.com...]

Now to attempt automatic mode, this does NOT work, help!

RewriteMap lowercase int:tolower
RewriteEngine on

RewriteCond ${lowercase:%{HTTP_HOST}}!^$
RewriteCond ${lowercase:%{HTTP_HOST}}!^www\.mydomain.com$

RewriteCond ${lowercase:%{HTTP_HOST}} ^(www\.¦)([^.]+)\.mydomain\.com$
RewriteRule ^(.+) ${lowercase:%{HTTP_HOST}}$1 [C]
RewriteRule ^(www\.¦)([^.]+)\.mydomain\.com.*) /$2$3 [L]

5:57 pm on Feb 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

>>Will it help to enable RewriteLog?

Yes it would. But let me first check the rules on my server. Once they work for me you could try them on your server. If they do not work there, then enabling RewriteLog [httpd.apache.org] will help indeed.

>>And what RewriteLogLevel is safe to use without
>>slowing down the serve?

RewriteLogLevel [httpd.apache.org] below 3 are ok. But they will not always yield enough information about what´s going on.

Let me finish dinner and I´ll test the rules ASAP ;)


7:35 pm on Feb 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Now I´m really confused. The rules no longer prevent recursion. This entry in my rewritelog is what´s at the heart of the problem and what is really baffling me.

RewriteCond: input='aaron<->aaron' pattern='!^(.*)<->\1$' => matched

What is says is basically that what is on the left hand side of the <-> is not the same as what´s on the right hand side. BUT IT IS THE SAME!

Perl [perl.com] is on my side.

Perl [perl.com] -e '$_="aaron<->aaron"; print "+" if /^(.*)<->\1$/;'

will print +. I know that is no real proof since Perl [perl.com] uses Perl [perl.com] regular expressions while mod_rewrite [httpd.apache.org] uses POSIX regular expressions but I´m using only a subset that is equal in both.

And what´s really strange is that the even the earlier code that worked for amznVibe and me no longer works :o

I switched from Redhat to Debian and Apache [httpd.apache.org] 1.27 to 1.26 but never expected that there were such big differences.

RewriteEngine On
# Rewrite only when Host is not empty
RewriteCond %{HTTP_HOST}!^$
# Rewrite only when Host is not main domain
# Delete if there is no main server that is not subject to rewriting
#RewriteCond %{HTTP_HOST}!^server$ [NC]
# Extract domain (%1), tld (%2) and first two path elements (%3)
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} \.([^.]+)\.([^.]+)<->/([^/]+)?/?([^/]+)? [NC]
# Rewrite only when "tld/domain" not equal to first two path elements
RewriteCond %2<->%3!^(.*)<->\1$ [NC]
RewriteCond %1<->%4!^(.*)<->\1$ [NC]
# Rewrite to /subdomain/request
RewriteRule ^(.*) /%2/%1/$1 [L]

This code does work except that it will cause an endless loop. You might want to try it and see whether it goes into a loop. It shouldn´t and didn´t in my earlier code when testing on Redhat.


This 32 message thread spans 2 pages: 32

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month