Dynadot .com transfer sale, only $10.12 with coupon code DYNA12, available while supplies last, ends October 31, 2026.

Subdomains with PHP and htaccess

Namecheap AuctionsNamecheap Auctions
Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
Watch

mignolo

Established Member
Impact
10
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
...

How I can obtain this with htaccess?
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
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:
$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];
}
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:
0
•••
If you use mod_rewrite don't forget to turn the RewriteEngine on and set a condition for rewrite :)
 
0
•••
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.
 
0
•••
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?

tres said:
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:
$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];
}
print $cat;


The above should assign 'test' to $cat using the URL's ww.test.example.com as well as test.example.com
 
0
•••
mignolo said:
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?

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/apache/mod_rewrite-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:
0
•••
AIVikingsAIVikings

We're social

Escrow.com
Spaceship
Escrowly
CryptoExchange.com
Domain Recover
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back