Raythe
Raythe

Reputation: 472

htaccess multiple subdomain to multiple url's

I'm not sure how to best approach this.

We have several salespeople here and each want a subdomain ie:

http://johndoe.mydomain.com  

However, we have all of their profiles on the site displayed dynamically, IE:

http://mydomain.com/sales/index.php?person=jdoe

We have about 15 salespeople, and eventually I will have time to script this out as new profiles are entered. For now, I can hand edit a .htaccess file if need be. All of the examples I have seen seem to redirect to a specific domain or subfolder. How should I approach this?

Thanks for the help!

Upvotes: 2

Views: 1175

Answers (1)

Mathieu Rodic
Mathieu Rodic

Reputation: 6762

You place this in the .htaccess file:

RewriteCond  %{REQUEST_FILENAME}  !-f
RewriteCond  %{HTTP_HOST}         ^(.+)\.mydomain\.com$         [NC]
RewriteRule  ^/?$                 /index.php?person=%1          [P,L,QSA]

This would be more generic:

RewriteCond  %{REQUEST_FILENAME}  !-f
RewriteCond  %{HTTP_HOST}         ^(.+)\.mydomain\.com$         [NC]
RewriteRule  ^.*$                 /index.php?person=%1&page=$0  [P,L,QSA]

The problem could also be solved at the PHP level:

$domain_array = explode(".", $_SERVER["HTTP_HOST"]);
$person = $domain_array[0];

Or:

if (preg_match("@^(.+)\.mydomain\.com$@", $_SERVER["HTTP_HOST"], $match)){
    $person = $match[1];
} else{
    $person = "";
}

Upvotes: 1

Related Questions