Subdomains driving me nuts

Using a single web hosting account to host multiple sites

Subdomains driving me nuts

Postby Fredev » Wed Jun 07, 2006 11:10 pm

I know, this question has been asked over and over, but still i can't find a working solution

Here's what i want

aa.domain.com ->rewrite to -> www.domain.com/aa
bb.domain.com ->rewrite to -> www.domain.com/bb
cc.domain.com ->rewrite to -> www.domain.com/cc
etc etc
I don't want the browsers address bar to display the rewritten URL but aa.domain.com.


Ofcourse www. should no be rewritten, nor an 'empty' domain (domain.com without subdomain).

if a scriptname , like index.htm, is append to the original url, this should also be appended to the rewritten url.

Regards,

Fré
Fredev
 
Posts: 15
Joined: Wed Jun 07, 2006 11:02 pm

Re: Subdomains driving me nuts

Postby Fredev » Wed Jun 07, 2006 11:25 pm

This is what i have so far
It does rewrite aa.domain.com/index.php to www.domain.com/aa/index.php, but it does no work without appending a filename to the URL. so aa.domain.com does'nt work

Second think, i the rewritten URL appears in the address bar


Options FollowSymLinks

RewriteEngine on

# Ignore http://www.domain.com
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]

# Must be a subdomain
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+)\.domain\.com$ [NC]

# A directory with the name of the subdomain must exist
RewriteCond %{DOCUMENT_ROOT}/%1 -d

# Add the requested hostname to the URI
# [C] means that the next Rewrite Rule uses this
RewriteRule ^(.+) %{HTTP_HOST}/$1 [C]

# Translate xyz.domain.com/foo to domain.com/xyz/foo
#RewriteRule ^([a-z0-9-]+)\.domain\.com/?(.*)$ http://www.domain.com/$1/$2 [L]
RewriteRule ^([a-z0-9-]+)\.domain\.com/ http://www.domain.com/$1 [L]
Fredev
 
Posts: 15
Joined: Wed Jun 07, 2006 11:02 pm

Postby richardk » Thu Jun 08, 2006 4:52 am

For Apache 2:
Code: Select all
RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.)?DOMAIN$    [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.DOMAIN$ [NC]
RewriteCond %1---%{REQUEST_URI} !^(.+)---/\1(/.*)?$
RewriteRule .* /%1%{REQUEST_URI} [QSA,L]


For Apache 1.3:
Code: Select all
RewriteEngine On

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


In both examples replace "DOMAIN" with your domain name. escape any "."s with a "\", eg. "google.com" becomes "google\.com".
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Fredev » Thu Jun 08, 2006 5:33 am

Great, it works!
But,.......
I don't exactly understand why.

I do understand the first condition
RewriteCond %{HTTP_HOST} !^(www\.)?DOMAIN$ [NC]
Only when subdomain is not www


Could you explain the other lines al litte (apache2)
Fredev
 
Posts: 15
Joined: Wed Jun 07, 2006 11:02 pm

Postby richardk » Thu Jun 08, 2006 6:11 am

Code: Select all
RewriteCond %{HTTP_HOST} ^([^\.]+)\.DOMAIN$ [NC]

This line grabs the subdomain name, eg. "richard" from "richard.DOMAIN" and puts it in the "%1" variable.

Code: Select all
RewriteCond %1---%{REQUEST_URI} !^(.+)---/\1(/.*)?$

This line checks that the subdomain name (from the "%1" variable) is not the same as the first directory with a backrefrence (the "\1") to stop an infinite loop. This is because once mod_rewrite has made the rewritten the url ("richard.DOMAIN/file.php" to "richard.DOMAIN/richard/file.php") it goes through mod-rewrite again, and would match again (without this RewriteCond) causing an infinite loop.

Code: Select all
RewriteRule .* /%1%{REQUEST_URI} [QSA,L]

This line creates the path "/richard/file.php".
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Fredev » Thu Jun 08, 2006 6:47 am

Thanks Richard, that explains a lot.

One more question.

aaa.domain.com gets translated to www.domain.com/aaa without changing the visible address, wich is OK


But, lots of people are use to ALWAYS tpye www in an URL , so the ure will be www.aaa.domain.com. In this case, nothing get rewritten?

Any ideas?

Regards,
fré
Fredev
 
Posts: 15
Joined: Wed Jun 07, 2006 11:02 pm

Postby richardk » Thu Jun 08, 2006 6:59 am

This should allow an optional www.:
Code: Select all
RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.)?SUB$ [NC]
RewriteCond %{HTTP_HOST}  ^(www\.)?([^\.]+)\.SUB$ [NC]
RewriteCond %2---%{REQUEST_URI} !^(.+)---/\1(/.*)?$
RewriteRule .* /%2%{REQUEST_URI} [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Fredev » Sun Jun 11, 2006 5:53 am

Ok, great, it works.
But is still don't exactly understand these 2 last lines


RewriteCond %2---%{REQUEST_URI} !^(.+)---/\1(/.*)?$ RewriteRule .*

/%2%{REQUEST_URI} [QSA,L]

Could you explain these lines a bit deeper? For instance, what does --- mean?

What would i need to change if i want to rewrite aaa.domain.com to www.domain.com/usr/aaa instead of www.domain.com/aaa?
Fredev
 
Posts: 15
Joined: Wed Jun 07, 2006 11:02 pm

Postby richardk » Sun Jun 11, 2006 7:05 am

For instance, what does --- mean?

It has no meaning, they are just seperating the variables.

Code: Select all
RewriteCond %2---%{REQUEST_URI} !^(.+)---/\1(/.*)?$

This is kind of a simplistic explaination, you might want to read this: http://www.regular-expressions.info/brackets.html (the "How to Use Backreferences" section).

First it makes a sting from the %2 variable (holding the name od the subdomain, eg. www.richard.dom.com, that was matched in the line above), then there are three dashes (just to seperate it) and finaly the requested uri (eg www.richard.dom.com/abc/def/). The final string would be "richard---/abc/def/".

Then on the other side, it creates another string, matching "richard" from the first string, and then putting it into the \1 place too, making "richard---/richard/*" (where * can be anything), as long as they don't match mod_rewrite will continue.

This is because once mod_rewrite has made the rewritten the url ("richard.DOMAIN/file.php" to "richard.DOMAIN/richard/file.php") it goes through mod-rewrite again, and would match again (without this RewriteCond) causing an infinite loop.


Code: Select all
RewriteRule .* /%2%{REQUEST_URI} [QSA,L]

.* matches everything, because if it is a subdomain, it will need applying to all paths.

%2 still has "richard" in it. and REQUEST_URI has "/abc/def/" in it, so it puts them together making "/richard/abc/def/", the real server path relative to your document root.

QSA appends the query string and L stops mod_rewrite.

What would i need to change if i want to rewrite aaa.domain.com to www.domain.com/usr/aaa instead of www.domain.com/aaa?

The code becaomes a little simpler.
Code: Select all
RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.)?SUB$ [NC]
RewriteCond %{HTTP_HOST}  ^(www\.)?([^\.]+)\.SUB$ [NC]
RewriteRule !^usr(/.*)?$ /usr/%2%{REQUEST_URI} [QSA,L]

The third RewriteCond is not needed. The Rewrite rule does not match requests for the /usr directory so infinite loops can't happen.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

It's still not working correctly!

Postby softtoy » Mon Jun 12, 2006 10:09 pm

I was using that richard's code above for Apache2(see below):
Code: Select all
RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.)?DOMAIN$    [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.DOMAIN$ [NC]
RewriteCond %1---%{REQUEST_URI} !^(.+)---/\1(/.*)?$
RewriteRule .* /%1%{REQUEST_URI} [QSA,L]


http://sub.domain.com/test/ works!
http://sub.domain.com/test does NOT work!

It's doing this:
http://sub.domain.com/test --> http://sub.domain.com/sub/test/

Directory structure is simply ../htdocs/sub/test/..

If I go to http://sub.domain.com/sub/test/ it goes to root, same as http://domain.com/sub/test/, it shouldn't. Is it even able to rewrite __WHOLE__ subdomain to folder __CORRECTLY__(you know what this means..), not going to directories/files in the root when using subdomain? I can't add new virtual host every time manually when there is new site somesub.domain.com, and when there are complete web sites under subdomain it MUST WORK.

I read that mod_rewrite cannot act below root, but I think it still should be able to do this one.

Anyone, any ideas?
softtoy
 
Posts: 26
Joined: Mon Jun 12, 2006 9:45 pm

Next

Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 86 guests

cron