Subdomains driving me nuts

Using a single web hosting account to host multiple sites

Postby kheldar » Sun Jul 09, 2006 1:28 pm

I have a further question regarding this technique I hope you don't mind my asking here...

I have a case where I run a number of websites, that all have a common structure. I use subdomains for different site areas, like support.domain.com and sales.domain.com or whatever.

I want to be able to use the SAME htaccess rule for every site, without having to manually go in and add "domain.com" to every htaccess file on every site.

So I would like some way to not only virtually map the subdomain to a corresponding directory, but also to have it automatically know what the domain it's on is, so I never have to hard-code it.

Can this be done? Try as I might I just can't wrap my head around regex and htaccess yet...

Thanks!
kheldar
 
Posts: 4
Joined: Sun Jul 09, 2006 11:37 am

Postby richardk » Sun Jul 09, 2006 1:44 pm

What mod_rewrite are you using?

You could use this:
Code: Select all
RewriteCond %{HTTP_HOST} ^([^\.]+)\.[^\.]+\.[^\.]+$

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

It matchs anything.anything.anything. It doesn't matter what the domain is (as long as it's only three parts).
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby kheldar » Sun Jul 09, 2006 2:01 pm

Wow thanks for that. Now I'm afraid I must really show my utter ignorance when it comes to mode_rewrite rules...

Is there a variable in there somewhere for the domain? If I were creating the string I want in shell for example, it would be like:

/home/*/public_html/*/

with of course the asterisks being the wildcards. What I'm after is something that rewrites this:

http://sales.domain.com

into this:

http://domain.com/sales/

but most importantly, a universal rule that will work NO MATTER WHAT DOMAIN IT'S RUNNING ON.

In this way, I could stick that htaccess rule into every one of my sites, and voila... appropriate rewriting on every site, for every subdomain.

I already have an extra record in my DNS Zones, so right now ANY subdomain rewrites to root, but that's only half the battle.

Thanks again so much!
kheldar
 
Posts: 4
Joined: Sun Jul 09, 2006 11:37 am

Postby richardk » Sun Jul 09, 2006 2:19 pm

What's wrong with what i gave you? You have that line and it doesn't matter what the domain is. You copy the .htaccess file into the root directory of each site and you're done.

There isn't a variable of the domain, there's HTTP_HOST, but that's the whole thing, including the subdomain, and there's SERVER_NAME, but as far as i can tell, it's the same as HTTP_HOST when running virtualhosts.

If you have only the subdomains you want setup in DNS, not wildcard DNS you can forget the domain completely and just use
Code: Select all
RewriteCond %{HTTP_HOST} ^([^\.]+)\.

it'll just match the first part of the host that do that.

What I'm after is something that rewrites this

Do you not have any mod_rewrite at all yet? There's plenty of examples in this thread.

Apache 2.x:
Code: Select all
Options +FollowSymLinks

RewriteEngine On

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

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


For Apache 1.3, replace this line:
Code: Select all
RewriteCond %1---%{REQUEST_URI} !^(.+)---/\1(/.*)?$

with this:
Code: Select all
Rewritecond %{ENV:REDIRECT_STATUS} ^$

and remove the block after "# mod_dir problem".
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby kheldar » Sun Jul 09, 2006 3:02 pm

I'm just trying to actually understand anything about the code before I implement it, but honestly there is not a single line that makes any sense to me. I can script all day in PHP but htaccess looks like Swahili to me.

I'll just try a few lines & mush 'em together and see what happens... thanks!
kheldar
 
Posts: 4
Joined: Sun Jul 09, 2006 11:37 am

Postby richardk » Sun Jul 09, 2006 3:37 pm

If you want to understand it i suggest you google for some mod_rewrite tutorials (or look here, or here).

There's also an explaination of a slightly different version of the code at the start of this thread (#5 and #9).

You can also ask specific questions about what you don't underatsnd.

PS. If you need more help, can you provide the version of Apache you are using, there's a difference with the regex support between 1.3 and 2.x.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby kheldar » Sun Jul 09, 2006 9:10 pm

I've read a ton of stuff on mod rewrite and regex... tutorials, articles, forums, FAQs... just don't get any of it. Very frustrating, I usually grasp just about anything new in no time at all, not so with regex and mod_rewrite. I learn best by doing, so I'll just start copying & pasting code snippets and see what happens... thanks for all the offers for help, I think I'm kinda on my own at this point. Unless you'd care to drop by and look over my shoulder... ;)
kheldar
 
Posts: 4
Joined: Sun Jul 09, 2006 11:37 am

Previous

Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 93 guests

cron