hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Web Hosting Talk Tutorials : Programming Tutorials : Script: Subdomain->Domain Redirect
Reply

Forum Jump

Script: Subdomain->Domain Redirect

Reply Post New Thread In Programming Tutorials Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 12-02-2005, 03:21 PM
Dan L Dan L is offline
Web Developer
 
Join Date: Feb 2003
Location: Connecticut
Posts: 5,441

Script: Subdomain->Domain Redirect


PHP Code:
<?php
    $domain 
$_SERVER['SERVER_NAME'];
    
$data $_SERVER['REQUEST_URI'];
    
    if(
fnmatch('*.*.*',$domain)) {
        
$domain explode('.',$domain);
        
$newDomain $domain[1].'.'.$domain[2].'/'.$domain[0].$data;
        
        
header('Location: '.$newDomain);
    } else {
        echo 
'Could not forward from this URL.';
    }
?>
This script takes a subdomain such as 'subdomain.example.com/index.php?a=test' and transforms it into 'example.com/subdomain/index.php?a=test'

This is helpful is you want to have subdomains point to a folder on your main domain.

Note: This does not support TLDs such as '.co.uk', '.com.cn', or any TLDs with two parts.



Sponsored Links
  #2  
Old 12-05-2005, 02:36 PM
Korvan Korvan is offline
Web Hosting Guru
 
Join Date: Nov 2005
Posts: 268
To support those TLDs simply adding a $domain[3] and another .* inside the fnmatch should work.
PHP Code:
<?php 
$domain 
$_SERVER['SERVER_NAME']; 
$data $_SERVER['REQUEST_URI']; 
 
if(
fnmatch('*.*.*.*',$domain)) { 
$domain explode('.',$domain); 
$newDomain $domain[1].'.'.$domain[2]'.'.$domain[3].'/'.$domain[0].$data
 
header('Location: '.$newDomain); 
} else { 
echo 
'Could not forward from this URL.'

?>


Last edited by Korvan; 12-05-2005 at 02:45 PM.
  #3  
Old 12-05-2005, 08:42 PM
Dan L Dan L is offline
Web Developer
 
Join Date: Feb 2003
Location: Connecticut
Posts: 5,441
As far as I know, that would only match .*.* extensions, not .*

Sponsored Links
  #4  
Old 12-06-2005, 01:14 PM
Korvan Korvan is offline
Web Hosting Guru
 
Join Date: Nov 2005
Posts: 268
Quote:
Originally Posted by DanX
As far as I know, that would only match .*.* extensions, not .*
Yes, that is correct, it will only support .co.uk and not .com for example.

Also considering peoples main subdomain is usually www you can code it like this for support of both types of TLD's

PHP Code:
$domain $_SERVER['SERVER_NAME']; 
$data $_SERVER['REQUEST_URI']; 
 
 
$domain explode('.',$domain); 
if(
$domain[0] != 'www')
{
$newDomain 'http://www.'.$domain[1].'.'.$domain[2].($domain[3] ? '.'.$domain[3] : '').'/'.$domain[0].$data
header('Location: '.$newDomain); 
echo(
"You are being redirected to <A HREF=\"{$newDomain}\">{$newDomain}</A>");
exit();
}
 
//continue page output 
This will only work if you use www to access the root of your site.

  #5  
Old 12-06-2005, 10:01 PM
Dan L Dan L is offline
Web Developer
 
Join Date: Feb 2003
Location: Connecticut
Posts: 5,441
Well the script was originally coded since someone wanted to redirect the same way cPanel does.

The echo is sort of pointless since nothing is output after the header.

Reply

Related posts from TheWhir.com
Title Type Date Posted
Mistakes To Avoid When Launching A New Website Design Blog 2014-01-10 10:53:50
Web Hosting Sales and Promos Roundup - December 6, 2013 Web Hosting News 2014-05-23 15:42:37
Without Court Orders, UK Police Demand Registrars Suspend Torrent Domains Web Hosting News 2013-10-10 11:51:53
OpenStack and the AWS API Debate Web Hosting News 2014-05-09 12:40:29
VeriSign Domain Industry Brief Finds 6 Million Domains Added in Q4 2012 Web Hosting News 2013-04-08 10:11:01


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:
WHT Membership
WHT Membership



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?