Subdomains driving me nuts

Using a single web hosting account to host multiple sites

Postby richardk » Tue Jun 13, 2006 6:55 am

It's a conflict with mod_dir and not really mod_rewrite's fault.

The best i can do is use mod_rewrite to remove the directory.
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]

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

how do I add 'sub'

Postby softtoy » Wed Jun 14, 2006 12:41 am

That one is working pretty well. The best one so far. Thx.

--Edit--
I thought I could handle the rest by myself but I can't understand that bs.

So richard(or anyone) please:

I have 'sub' folder for subdomains, subs not directly under root.
How do I add folder 'sub' correctly to that code? I've tried but er500 always. It should also correctly check if sub/subdomain exists.
Dir structure: htdocs/sub/subdomain

This is already checking sub/subdomain existence. How to add the 'sub'?
Code: Select all
RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$    [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteCond c:/progra~1/apache~1/apache2/htdocs/sub/%1 -d
RewriteCond %1---%{REQUEST_URI} !^(.+)---/\1(/.*)?$
RewriteRule .* %1%{REQUEST_URI} [QSA,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$    [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteCond %1---%{REQUEST_URI} ^(.+)---//?\1(/.*)?$
RewriteRule .* %2 [R=301,QSA,L]
softtoy
 
Posts: 26
Joined: Mon Jun 12, 2006 9:45 pm

Postby richardk » Wed Jun 14, 2006 3:17 am

You want to send richard.domain.com to /sub/richard/?

There was an example earlier in this thread (without the trailing slash fix), the bottom of this post.

This should work:
Code: Select all
RewriteEngine On

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

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\.)?DOMAIN$ [NC]
RewriteCond %{HTTP_HOST}  ^(www\.)?([^\.]+)\.DOMAIN$ [NC]
RewriteCond %2---%{REQUEST_URI} ^([^\.]+)---/sub/\1(/.*)?$
RewriteRule ^sub(/.*)?$ http://%1.DOMAIN%2 [R=301,L]

"sub" is the directory, "DOMAIN" is your domain.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

thanks

Postby softtoy » Wed Jun 14, 2006 6:44 am

There was an example earlier
oops.

I could never(with that example maybe) figure that nonsense out by myself even though I am pro-coder.

Thank you so much. Perfect.
softtoy
 
Posts: 26
Joined: Mon Jun 12, 2006 9:45 pm

Postby Uri » Thu Jun 15, 2006 9:41 am

Hi,

First of all I'd like to thank you, espacially richard, for giving this code, it has helped me a lot!

I just want to point 2 things:

At the moment I'm using this:

RewriteEngine On

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

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com$ [NC]
RewriteCond %2---%{REQUEST_URI} ^(.+)---//?\1(/.*)?$
RewriteRule .* %3 [R=301,QSA,L]

I changed the %1 to %2 and %2 to %3 to allow the writing of www.subdomain.domain.com.

What I'd like now, if someone types www.subdomain.domain.com, how can I redirect him to subdomain.domain.com to notice him that this one is the correct, not with the www. I suppose it shouldn't be much different from what I have now, but I'm absolutely dumb at this.

Thanks!
Uri
 
Posts: 3
Joined: Wed May 31, 2006 7:52 am

Postby richardk » Thu Jun 15, 2006 11:02 am

What I'd like now, if someone types www.subdomain.domain.com, how can I redirect him to subdomain.domain.com to notice him that this one is the correct, not with the www.

This should do it:
Code: Select all
RewriteEngine On

# match www.sub.domain.com
RewriteCond %{HTTP_HOST} ^www\.([^\.]+\.DOMAIN)$ [NC]
# redirect to sub.domain.com
RewriteRule .* http://%1%{REQUEST_UTI} [R=301,L]

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

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

Postby Uri » Mon Jun 19, 2006 8:13 am

Thank you very much richard, it works great!
Uri
 
Posts: 3
Joined: Wed May 31, 2006 7:52 am

Postby iGeek » Sun Jun 25, 2006 5:34 pm

This has to be one of the most useful topics I've ever read!

Quick question. Using the advice here I have the following setup:

Code: Select all
RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.)?mydomain\.com$ [NC]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^\.]+)\.mydomain\.com$ [NC]
RewriteRule .* /file.php?sub=%1% [QSA,L]


For the Rewrite rule I am clearly accessing the page "file.php" because the error I have from PHP. The problem is the %1% does not equal the subdomain. Is my rule wrong? If I do this does PHP recognize this as a $_REQUEST?

If I type in the URL /file.php?sub=subdomain it works, no errors. I'm so close!

I GOT IT [EDIT]

One too many % signs in the rewrite rule.
iGeek
 
Posts: 1
Joined: Sun Jun 25, 2006 5:29 pm

Postby softtoy » Thu Jul 06, 2006 1:55 am

www.subdomain.domain.com

That is stupid address anyway. I'm using that code but added wildcard there so it will catch all including www. Just modify it a bit: ".*" is wildcard.
softtoy
 
Posts: 26
Joined: Mon Jun 12, 2006 9:45 pm

Postby richardk » Thu Jul 06, 2006 7:08 am

If you mean you've done
Code: Select all
RewriteCond %{HTTP_HOST} ^(.*)\.DOMAIN$ [NC]

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

or
Code: Select all
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com$ [NC]

it's different because www.sub.domain.com (/www.sub/ or /file.php?sub=www.sub) will take you to a different place than just sub.domain.com (/sub/ or /file.php?sub=sub).
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

PreviousNext

Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 101 guests

cron