Jump to content

Dynamic Subdomain Redirect


kmzacks

Recommended Posts

Howdy all; is there any way I can set up a dynamic subdomain redirect without having to configure each individual subdomain?

 

Specifically, what I'm looking to do is redirect any subdomain on my domain to default.php?siteid=SUBDOMAIN. E.g.:

 

site1.mysite.com would redirect to www.mysite.com/default.php?siteid=site1

site2.mysite.com would redirect to www.mysite.com/default.php?siteid=site2

site5678.mysite.com would redirect to www.mysite.com/default.php?siteid=site5678

... and so on.

 

I know I can easily set up subdomains and redirects via cPanel, but I'd like to not have to do this for every single subdomain, and I would also like it to be dynamic so that it redirects properly regardless of the "subdomain" that's accessed. Is there a way to do this just using .htaccess, or does it also involve a DNS change?

 

Thanks in advance for any help y'all can provide.

Link to comment
Share on other sites

I think this would require at least a wildcard record in DNS and probably Apache. The help desk should be able to tell you for sure if it's doable. Once you get the addresses resolving to your site, the redirects can be done using a RewriteRule in .htaccess.

Link to comment
Share on other sites

I've created a similar thing for a project on a TCH server, we call the system 'clone sites'.

 

Now we have a folder in the root called 'clone sites' in which we include all files that are needed by the subdomains, then we place a single file called index.php in the subdomain folder, and use .htaccess to rewrite to the clone sites folder.

 

Try this:

 

.htaccess file

Options +FollowSymlinks

RewriteEngine On

 

RewriteRule ^(.*).php$ index.php?page=$1 [nc,L]

 

index.php

<?php

If (Empty($_GET['page']))

include('../clonewebsites/index.php');

Else

include('../cloneWebsites/' . $_GET['page'] . '.php');

?>

 

This should do the same job.

 

Regards

Dave

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...