using specific subdomains in the rewrite?

Using a single web hosting account to host multiple sites

using specific subdomains in the rewrite?

Postby therealjlo » Sun Jun 21, 2009 6:43 pm

Here's what I'm trying to do:

http://foo.domain.com/bar
redirects without changing the browser to:
http://www.domain.com/foo.php?this=bar

and

http://foo.domain.com/bar/whee.html
redirects without changing the browser to:
http://www.domain.com/foo.php?this=bar&other=whee

I have wildcard subdomains set up and I almost have it with the following setup. It just doesn't catch the specific subdomain and translate it to that "subdomain.php" script:

Code: Select all
RewriteEngine On

RewriteRule ^$ /foo.php [QSA,L]
RewriteRule ^([^/\.]+)$ /foo.php?this=$1 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)\.html$ /foo.php?this=$1&other=$2 [QSA,L]


But that only works because every subdomain is going to the foo.php script. So "http://narf.domain.com/bar/whee.html" is also going to "http://www.domain.com/foo.php?this=bar&other=whee" when I actually will want that going to "http://www.domain.com/narf.php?thisothervar=bar&somethingelse=whee"

And, of course, I want "www." to just go to the main index.php page. :)

So, can specific subdomains be used to catch the url? Something like:

Rewrite Cond (If foo.domain.com)
RewriteRule ^([^/\.]+)$ /foo.php?this=$1 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)\.html$ /foo.php?this=$1&other=$2 [QSA,L]

Rewrite Cond (if narf.domain.com)
RewriteRule ^([^/\.]+)$ /narf.php?this=$1 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)\.html$ /narf.php?thisothervar=$1&somethingelse=$2 [QSA,L]

Then deal with anything else?

Many thanks for pointing me in the right direction.

Edit: Changed subject to clarify I'm talking about "specific" subdomains and not just catching "any" subdomain.
Last edited by therealjlo on Mon Jun 22, 2009 12:28 pm, edited 1 time in total.
therealjlo
 
Posts: 11
Joined: Tue Aug 05, 2008 9:37 am

Postby richardk » Mon Jun 22, 2009 7:59 am

FAQ: Virtual sub domains.

Match the sub domain with a RewriteCond and the %{HTTP_HOST} variable.
Code: Select all
# The sub domain will be in %2.
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]

If you didn't want to allow an optional www., you could use
Code: Select all
# The sub domain will be in %1.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]

(Note that it's now %1 and the below rules would need updating.)

Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Stop processing here if it's example.com or www.example.com.
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule .* - [L]

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^$ /%2.php [QSA,L]

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^([^/\.]+)$ /%2.php?this=$1 [QSA,L]

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^([^/\.]+)/([^/\.]+)\.html$ /%2.php?this=$1&other=$2 [QSA,L]


You might even be able to combine it into one rule
Code: Select all
[code]Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^([^/\.]+)(?:/(?:([^/\.]+)\.html)?)?$ /%2.php?this=$1&other=$2 [QSA,L][/code]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby therealjlo » Mon Jun 22, 2009 9:53 am

Thank you so much, richard. I'll give this a shot and report back. I was doing my best walking through little changes to see what happened and I just kept making things worse. :)

*crossing my fingers!*
therealjlo
 
Posts: 11
Joined: Tue Aug 05, 2008 9:37 am

Postby therealjlo » Mon Jun 22, 2009 12:26 pm

OK, that almost did it, but it definitely helped me get to where I needed. Here's what i ended up with.

Keeping in mind that right now, we have specific things for the subdomains "foo" and "bar". They go to "foo.php" and "bar.php" respectively but that's just for our internal use. They could go to "index.php?action=foo" and "index.php?action=bar" if we wanted them to.

For other subdomains, we may want to do other things. So, instead of using the %1 or %2, I just ended up putting them in directly. I probably should have figured this out before, but it just didn't click.

Here's what we ended up with:

Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Stop processing here if it's example.com or www.example.com.
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule .* - [L]

# These specifically deal with subdomain "foo"
RewriteCond %{HTTP_HOST} ^foo\.example\.com$ [NC]
RewriteRule ^$ /foo.php [QSA,L]

RewriteCond %{HTTP_HOST} ^foo\.example\.com$ [NC]
RewriteRule ^([^/\.]+)$ /foo.php?this=$1 [QSA,L]

RewriteCond %{HTTP_HOST} ^foo\.example\.com$ [NC]
RewriteRule ^([^/\.]+)/([^/\.]+)\.html$ /foo.php?this=$1&other=$2 [QSA,L]

# These specifically deal with subdomain "bar"
RewriteCond %{HTTP_HOST} ^bar\.example\.com$ [NC]
RewriteRule ^$ /bar.php [QSA,L]

RewriteCond %{HTTP_HOST} ^bar\.example\.com$ [NC]
RewriteRule ^([^/\.]+)$ /bar.php?this=$1 [QSA,L]

RewriteCond %{HTTP_HOST} ^bar\.example\.com$ [NC]
RewriteRule ^([^/\.]+)/([^/\.]+)\.html$ /bar.php?thisothervar=$1&somethingelse=$2 [QSA,L]


Thanks again!
therealjlo
 
Posts: 11
Joined: Tue Aug 05, 2008 9:37 am

Postby richardk » Mon Jun 22, 2009 5:37 pm

You could still combine the rules
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^foo\.example\.com$ [NC]
RewriteRule ^([^/\.]+)(?:/(?:([^/\.]+)\.html)?)?$ /foo.php?this=$1&other=$2 [QSA,L]

RewriteCond %{HTTP_HOST} ^bar\.example\.com$ [NC]
RewriteRule ^([^/\.]+)(?:/(?:([^/\.]+)\.html)?)?$ /bar.php?thisothervar=$1&somethingelse=$2 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 19 guests

cron