Jump to content

Cleanest way to have wildcard subdomain mirror a domain


fitguydan

Recommended Posts

My service allows users to generate subdomains based off their username(testguy.mydomain.com). I've created a wildcard subdomain for this, so the dynamic subdomains work wonderfully.

 

I've added a parked domain where my domain is supposed to mirror the given subdomain. However, when I go to the domain.com, it redirects to the subdomain, changing the URL in the address bar. Any ideas?

 

Or if there a better way to mirror a subdomain while showing the original domain in the address bar?

Edited by fitguydan
Link to comment
Share on other sites

Apache does not have the suPHP module loaded. It's that simple. Talk to your hosting company to see what's up with it.

 

Haha, I actually fixed that initial problem from my thread and edited my post to a different, but similar problem. 

Link to comment
Share on other sites

UPDATE:

 

I found some code that appears to work exactly as I wanted, but since I'm very new to this process, is this a bad way to do it? Or a better question: is there a better or more efficient way to do this?

 

Basically, I added the domain.com to my server through WHM. And in the root directory of the newly added domain, the only file is the htaccess file and in it(among a few security lines), there is:

 

RewriteCond %{HTTP_HOST} ^(www.)?newdomain\.com$ [NC]
RewriteRule ^(.*)$ http://test.othersite.com/$1 [P,L,NC,QSA]
 
It works, but like I said, is there a cleaner or better way to do this?
Link to comment
Share on other sites

There would only be a redirect if there was something telling Apache (or maybe it's in your code?) to do so. I don't know enough about your system to tell you where that is. If it's not code then often it's in a .htaccess; in this case there would be something very much like what you've just posted:

RewriteCond <if the HTTP_HOST is not the subdomain>
RewriteRule <to the subdomain>
The domain would have to be pointing (DocumentRoot) to the same location as the subdomain, thus the .htaccess would kick in and redirect.

If it's in code then it could be anything, anywhere.

 

Now I'm kinda lost to what the problem is. You were talking about domain->subdomain redirecting being a problem. The stuff you posted redirects everything from not newdomain.com to test.othersite.com (and I have no idea which of those domain names correspond to what sites).

 

So let's backtrack a bit.

 

You have a parked domain. Let's call it "parkeddomain.com".

 

1. Being parked, there won't be any content there, and instead it'll (apparently) redirect?

2. Does this parked domain have subdomains? Just to be sure. I don't think it does.

3. What is its purpose if all it does is redirect?

 

You also have a regular domain, "domain.com", and per-user subdomains, say "user.domain.com".

 

4. What are the redirects? From which site(s) to which other site(s)?

5. What do you already have in place for your .htaccess files and/or virtualhost configurations?

6. What is it doing now that you don't want?

Link to comment
Share on other sites

Sorry for the confusion, I'll address your questions:

 

I have a main website that let's users have their own custom.mainsite.com. Along with that, their pages are at custom.mainsite.com/blog or anything else after the '/'

 

I need an added feature that lets a user register their own domain(any registrar.. doesn't matter), point the nameservers to my server(I would give directions on how they can do that), and then I take the 3rd step and make it so that when someone visits theirdomain.com, it shows the content of custom.mainsite.com, but without changing the address bar. Similarly,I want any other pages like theirdomain.com/blog to show the content of custom.mainsite.com/blog.

 

Wordpress and wix(and many others) do this, but I don't know WP(and obviously can't see how wix codes their custom domains) so I just don't know the best way to accomplish this. I have a working way, which I posted above, but since I'm new to this process, I may be making it way harder on myself or the server with the method I used.

Edited by fitguydan
Link to comment
Share on other sites

The setup kind of depends on what all your hosting and how. For the wildcard subdomains you can setup a wildcard virtualhost in apache to respond, then in your code check $_SERVER['HTTP_HOST'] to determine which domain is being accessed:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias *.example.com
    DocumentRoot /var/www/example.com/public_html
</VirtualHost>
And in PHP:

<?php
//Assumes only one level
list($subdomain) = explode('.', $_SERVER['HTTP_HOST'], 2);
var_dump($subdomain);
?>
To allow users to attach their own custom domains you'd need to add additional ServerAlias directives for each additional domain.

 

Alternatively, if these user sites are the only thing you plan to host on the server you can just configure apache as a single-host server and point all requests to the same codebase regardless of how the request was made. Then you can use PHP as indicated above to inspect the hostname used and respond appropriately.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.