handling subdomain redirects?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jrucifer
Forum Commoner
Posts: 32
Joined: Fri May 13, 2005 2:36 pm
Location: phoenix

handling subdomain redirects?

Post by jrucifer »

I have a wildcard subdomain pointing to my site. My question is, can I run a PHP script to pull the subdomian from the URL?

So if the URL is http://username.domain.com, pull "username" and set it equal to a variable?

Is this possible, or do I have to go crazy setting up mod_rewrite redirects?

P.S., I would prefer to keep the subdomain in the URL throughout navigation.

Thanks in advance for any help!
jrucifer
Forum Commoner
Posts: 32
Joined: Fri May 13, 2005 2:36 pm
Location: phoenix

Post by jrucifer »

Okay, I decided to stop being lazy and try to figure it out on my own a little more...

what I came up with is pretty... shotty, but it works.

Code: Select all

$host = $HTTP_HOST;
$names = explode(".", $host);
foreach($names as $name) {
if($name == "www" or $name == "domain" or $name == "com") {
continue;
}
else {
$name = $name;
}
Is this the most logical way, or is there something better?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

setting up mod_rewrite redirects
P.S., I would prefer to keep the subdomain in the URL throughout navigation.
Well, with certain uses of mod_rewrite you're not doing any redirecting per se. And by setting it up, you can keep the sub domain in the URL, plus send a customized variable of whatever it is in the sub domain part.

No extra code would be needed for URL parsing.

At first it may be daunting, but it's really not so bad once you get into it. We have a forum setup with examples and such regarding mod_rewrite. So if you chose to use that, be sure to post there if you get answers and I'm sure you'll get answered in no time. :)
Post Reply