Catch all subdomains and route to main domain

Using a single web hosting account to host multiple sites

Catch all subdomains and route to main domain

Postby therealjlo » Tue Aug 05, 2008 9:52 am

We've recently made some changes and would like to catch all previous subdomains and route them to the main "www.domain.com" while keeping the existing mod rewrite rules.

Everything i've seen shows how to map these to a folder with the same subdomain, but that's not what we want. Can this be done?

Here's the existing .htaccess:

Code: Select all
RewriteEngine On

RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteRule ^$ /default.php [QSA,L]
RewriteRule ^([^/\.]+)$ /default.php?a=$1 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)$ /default.php?action=$1&b=$2 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)$ /default.php?action=$1&b=$2&c=$3 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)$

/default.php?action=$1&b=$2&c=$3&d=$4 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([0-9]+).html$

/default.php?action=$1&b=$2&c=$3&d=$4 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([0-9]+).html$

/default.php?action=$1&b=$2&c=$3&d=$4&e=$5 [QSA,L]


So what we'd like is for
example.com/thissection/thispage/whatever.html
www.example.com/thissection/thispage/whatever.html
forums.example.com/thissection/thispage/whatever.html
users.example.com/thissection/thispage/whatever.html

All to end up at
www.example.com/thissection/thispage/whatever.html

Which is what happens right now just for
example.com/thissection/thispage/whatever.html and
www.example.com/thissection/thispage/whatever.html


Any help is appreciated. I know just enough mod rewrite to screw up the entire site.
therealjlo
 
Posts: 11
Joined: Tue Aug 05, 2008 9:37 am

Postby richardk » Wed Aug 06, 2008 3:06 pm

What happens now when you go to a sub domain (forums)?
Do you want a visible redirect?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby therealjlo » Wed Aug 06, 2008 4:13 pm

Richard, it was actually returning an error page.

I finally gave up and contacted Dreamhost and they had made a mistake when they enabled the wildcard subdomains. That's fixed.


Now, when I put in
forums.example.com/thissection/thispage/whatever.html
it returns
www.forums.example.com/thissection/thispage/whatever.html


I would like for it to NOT be a visible redirect, but to retain the
forums.example.com/thissection/thispage/whatever.html
in the browser but to just be using the
example.com/thissection/thispage/whatever.html
page

Thank you!
therealjlo
 
Posts: 11
Joined: Tue Aug 05, 2008 9:37 am

Postby richardk » Wed Aug 06, 2008 4:16 pm

But what directory on the server does the forums sub domain go to? /forums?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby therealjlo » Wed Aug 06, 2008 4:27 pm

They don't have subdirectories. I want them all to basically ignore the subdirectory. In the event that one has a subdirectory (forums will, but the others won't) I think I can catch that individually. We'd like to leave the subdomain visible for SEO purposes though.
therealjlo
 
Posts: 11
Joined: Tue Aug 05, 2008 9:37 am

Postby richardk » Thu Aug 07, 2008 12:46 pm

So where on the server do they currently go to? I need to know the current behaviour so that i can try and help change it.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby therealjlo » Thu Aug 07, 2008 3:05 pm

Thanks, Richard. I think you're helping me to ask the question better. I might be able to get it, but I keep having other issues come up.

Check the mod rewrite above. Right now, I run everything off of the dynamic default.php. I have it doing something like

example.com/search/face/1
/default.php?action=search&term=face&page=1

and internally get "a" and use it to determine what else to do. An "a=search" tells me to search through the site for "b" which is "face" and return the first page (c=1)

or

example.com/user/john/locker/3
/default.php?a=user&b=john&c=locker&d=3

which tells me to go to the "user" table, and get the "third" "locker" message for "john"

which is fine and how it works now. It takes those "subdirectories" in the url and converts them to the url parameters for default.asp.

So, what I'd also like is to be able to do

search.example.com/face/1
or
user.example.com/john/locker/3


So, does that help? Like I said, I might be able to do it. I'll play a little.
therealjlo
 
Posts: 11
Joined: Tue Aug 05, 2008 9:37 am

Postby richardk » Fri Aug 08, 2008 8:08 am

What i need to understand is how the sub domains currently work.
Now, when I put in
forums.example.com/thissection/thispage/whatever.html
it returns
www.forums.example.com/thissection/this ... tever.html

But what happens after that? www.forums.exmaple.com must go to some place on the server, a directory somewhere. What page do you see? Is it a page from your site?

After reading it again, i think it might be your main document root (the same place as example.com), so try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(example\.com)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

RewriteRule ^$ /default.php [QSA,L]
RewriteRule ^([^/\.]+)$ /default.php?a=$1 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)$ /default.php?action=$1&b=$2 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)$ /default.php?action=$1&b=$2&c=$3 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)$ /default.php?action=$1&b=$2&c=$3&d=$4 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([0-9]+).html$ b/default.php?action=$1&b=$2&c=$3&d=$4 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([0-9]+).html$ /default.php?action=$1&b=$2&c=$3&d=$4&e=$5 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Domain Handling

Who is online

Users browsing this forum: Google [Bot] and 22 guests

cron