NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Subdomains with PHP and htaccess

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search
5 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 05-30-2007, 02:52 PM THREAD STARTER               #1 (permalink)
NamePros Regular
 
mignolo's Avatar
Join Date: Mar 2007
Posts: 460
mignolo is a jewel in the roughmignolo is a jewel in the roughmignolo is a jewel in the rough
 



Question Subdomains with PHP and htaccess


I've many domains running on the same script with htaccess and php; this is the scheme:
index.php running on www.maindomain.com, then
www.domain1.com -> index.php?dn=domain1.com
www.domain2.com -> index.php?dn=domain2.com
www.domain3.com -> index.php?dn=domain3.com
...

Now I want this:
subdomain1.domain1.com -> index.php?dn=domain1.com&cat=subdomain1
subdomain2.domain1.com -> index.php?dn=domain1.com&cat=subdomain2
...
subdomain1.domain2.com -> index.php?dn=domain2.com&cat=subdomain1
subdomain2.domain2.com -> index.php?dn=domain2.com&cat=subdomain2
????: NamePros.com http://www.namepros.com/programming/333587-subdomains-with-php-and-htaccess.html
...

How I can obtain this with htaccess?
mignolo is offline  
Old 05-30-2007, 05:56 PM   #2 (permalink)
You are here: X
 
mr-x's Avatar
Join Date: Oct 2003
Location: US
Posts: 483
mr-x is a glorious beacon of lightmr-x is a glorious beacon of lightmr-x is a glorious beacon of lightmr-x is a glorious beacon of lightmr-x is a glorious beacon of light
 




I dont' have a host to test this but I off the top of my head,

RewriteRule (.*).domain1.com index.php?dn=domain1.com&cat=$1

The above won't work but another solution is possible, grab the sub-domain using php,

PHP Code:
$uri_parts explode('.',$_SERVER['HTTP_HOST']);
print_r($uri_parts);

// check if a sub-domain is included, 
if (count($uri_parts == 3) && $uri_parts[0] != 'www')
{
     
$cat $uri_parts[0];
}
else if (
count($uri_parts >3))
{
     
$cat = ($uri_parts[0] != 'www') ? $uri_parts[0] : $uri_parts[1];
????: NamePros.com http://www.namepros.com/showthread.php?t=333587
}
print 
$cat

The above should assign 'test' to $cat using the URL's ww.test.example.com as well as test.example.com
Last edited by mr-x; 05-30-2007 at 07:24 PM.
mr-x is offline  
Old 06-01-2007, 11:45 AM   #3 (permalink)
NamePros Regular
 
monaco's Avatar
Join Date: Jul 2005
Location: Tucson, AZ
Posts: 689
monaco will become famous soon enough
 



If you use mod_rewrite don't forget to turn the RewriteEngine on and set a condition for rewrite
__________________
My Website | My Blog
monaco is offline  
Old 06-03-2007, 10:16 AM   #4 (permalink)
Danltn.com
 
Daniel's Avatar
Join Date: May 2007
Location: Danltn.com / Nottingham, UK
Posts: 1,201
Daniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond repute
 


Ethan Allen Fund Ethan Allen Fund
Hi there, what Control Panel do you use? I have a script which creates email forwarders for Cpanel, which is easily changable to sub domains. Not sure if this is what you want though.
Daniel is offline  
Old 06-04-2007, 07:08 AM THREAD STARTER               #5 (permalink)
NamePros Regular
 
mignolo's Avatar
Join Date: Mar 2007
Posts: 460
mignolo is a jewel in the roughmignolo is a jewel in the roughmignolo is a jewel in the rough
 



I have Cpanel on Godaddy VPS

I should post "domain1.com" in the query, but it's a variable too:
(.*).(.*).com
Is it possible for htaccess to select the domain name that isn't always the same?
????: NamePros.com http://www.namepros.com/showthread.php?t=333587

Originally Posted by tres
I dont' have a host to test this but I off the top of my head,

RewriteRule (.*).domain1.com index.php?dn=domain1.com&cat=$1

The above won't work but another solution is possible, grab the sub-domain using php,

PHP Code:
$uri_parts explode('.',$_SERVER['HTTP_HOST']);
print_r($uri_parts);

// check if a sub-domain is included, 
if (count($uri_parts == 3) && $uri_parts[0] != 'www')
{
     
$cat $uri_parts[0];
}
else if (
count($uri_parts >3))
{
     
$cat = ($uri_parts[0] != 'www') ? $uri_parts[0] : $uri_parts[1];
????: NamePros.com http://www.namepros.com/showthread.php?t=333587
}
print 
$cat

The above should assign 'test' to $cat using the URL's ww.test.example.com as well as test.example.com
mignolo is offline  
Old 06-06-2007, 03:31 PM   #6 (permalink)
You are here: X
 
mr-x's Avatar
Join Date: Oct 2003
Location: US
Posts: 483
mr-x is a glorious beacon of lightmr-x is a glorious beacon of lightmr-x is a glorious beacon of lightmr-x is a glorious beacon of lightmr-x is a glorious beacon of light
 



Originally Posted by mignolo
I have Cpanel on Godaddy VPS

I should post "domain1.com" in the query, but it's a variable too:
????: NamePros.com http://www.namepros.com/showthread.php?t=333587
(.*).(.*).com
Is it possible for htaccess to select the domain name that isn't always the same?
You can use %{HTTP_HOST} in .htaccess. In PHP, $_SERVER['HTTP_HOST'] will have the same data.

Here is a cheat sheet for mod_rewrite, http://www.ilovejackdaniels.com/apac...e-cheat-sheet/

for what it's worth, the main problem I've had redirecting multiple domains to one script with mod_rewrite is recursion.

I think it's easier to let mod_rerwrite hand the normal stuff like friendly URL's and grab the host header from $_SERVER['HTTP_HOST']. Take a look at http://php.net/parse_url for some examples
Last edited by mr-x; 06-06-2007 at 04:12 PM.
mr-x is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 02:20 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger