Jump to content

Dynamic Subdomain Redirect

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.

Share this post

Link to post
Share on other sites

Welcome to the forums kmzacks :)


Sorry, I don't know of a way but someone may.

Share this post

Link to post
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.

Share this post

Link to post
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]




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



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



This should do the same job.




Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now