Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to blogger/wordpress/tumlr allow users to mask url

I am creating a saas application. Users have their own url like user.mysaasapp.com

To allow user to have own url, I use mod rewrite. Something like http://mysaasapp.com/?user=user to user.mysaasapp.com (This is working perfectly)

Now, I want to give user the flexibility to mask their url. This means that user can direct their subdomain (saas.theirdomain.com) to user.mysaasapp.com. How do i go about doing so.

I read many articles(cname) on this but still have not have a definite solution. I am wondering how big companies like wordpress/google does that.

e.g http://www.tumblr.com/docs/en/custom_domains

e.g http://developer.uservoice.com/docs/site/domain-aliasing/

I realize that companies would take this process:

  1. Get user to add cname
  2. Get user to enter their domain in the saas app (maybe under custom domain)

What is needed from me the get this working? Thanks!

like image 344
Slay Avatar asked Jul 28 '12 18:07

Slay


2 Answers

if Im right you can use wild card in ServerAlias and with mod_rewrite you can rewrite it to the right file so your vhosts gets like

<VirtualHost *:80>
    ServerName mysaasapp.com
    ServerAlias *.mysaasapp.com
    DocumentRoot /var/www/user
</VirtualHost>

and your CNAME gets like

*.mysaasapp.com IN CNAME mysaasapp.com

I hope this helps

EDIT:

change the vhosts to

<VirtualHost *:80>
    ServerName mysaasapp.com
    ServerAlias *.mysaasapp.com
    DocumentRoot [STANDARDFOLDER]
</VirtualHost>

and replace [STANDARDFOLDER] with the DocumentRoot of mysaasapp.com

and in your index.php place on the top this:

// Array with all the subdomains who doesn't belong to users
$_notUsers = array("www");
// No code without a Boom,
$_domainParts = explode('.',$_SERVER[HTTP_HOST],-2);
// Check if subdomain is legit
if(!in_array($_domainParts[0],$_notUsers) && !empty($_domainParts)){
    // get the real subdomain structure
    $_sub = str_replace('.mysaasapp.com','',$_SERVER['HTTP_HOST']);
    // Don't look at this it's horrible :'( but this makes it think that there is 
    // something in user 
    $_GET['user'] = $_sub;
    // I dont know where you want it in the question its index.php and in comment its 
    // user.php so change this as you want it
    include 'user.php';
    // Exit so we dont have an user page and homepage together but only when
    // using user.php comment this out when using index.php
    exit;
// end
}

this is a really dirty solution to your problem I hope it works for you now

UPDATE:

at first I didn't know you also want to redirect from a custom domain,

then they have to add a CNAME to their DNS server/Manager with

theirdomain.com IN CNAME thierusername.mysaasapp.com

and if your DNS records are so that all the subdomain are going to your apache should it be working with the code and the Vhosts I gave you

UPDATE 2:

lanzz just pointed out something I forgot (facepalm) im checking the HOST header what makes no sense because it will have customdomain.com so you need to ask your users for their domains (if you dont have a shared IP let them point with a CNAME and if you dont have a shared IP you can let them point to your IP with an A record) and lookup the domain from HTTP_HOST and search for that in your database after that it should work!

little bit of edited code:

//first look if you're on your own domain,
// also again an Array with all the subdomains who doesn't belong to users
$_notUsers = array("www");
//lets explode the domain 
$_domainParts = explode(".",$_SERVER["HTTP_HOST"]);
//get length of $_domainParts
$_domainPartsLength = count($_domainParts);
//get last two parts (little bit ugly but there are uglier ways)
$_currentDomain = $_domainParts[$_domainPartsLength-1] . "." . $_domainParts[$_domainPartsLength-2];
//time to check if it's yours and if it's legit
if($_currentDomain == "mysaasapp.com"){
    if(!in_array($_domainParts[0],$_notUsers) && !empty($_domainParts)){
        // get the real subdomain structure
        $_sub = str_replace('.mysaasapp.com','',$_SERVER['HTTP_HOST']);
        // Don't look at this it's horrible :'( but this makes it think that there is 
        // something in user 
        $_GET['user'] = $_sub;
        // I dont know where you want it in the question its index.php and in comment 
        // its user.php so change this as you want it
        include 'user.php';
        // Exit so we dont have an user page and homepage together but only when
        // using user.php comment this out when using index.php
        exit;
    }
    // here is your standard index (sorry for the odd placing)
}else{
    //now here are we gonna play with the custom domain
    // this function should return the username if the hostname is found in the
    // database other wise it should return false
    $_user = userGetByDomain($_SERVER["HTTP_HOST"]);
    if($_user === false){
        // tell the browser its not legit
        header("Status: 404 Not Found");
        //show your 404 page
        include '404.php';
        // and die
        exit;
    }
    // now we know everything is okay we are gonna do the same thing as above
    $_GET['user'] = $_user;
    // I dont know where you want it in the question its index.php and in comment 
    // its user.php so change this as you want it
    include 'user.php';
    // Exit so we dont have an user page and homepage together but only when
    // using user.php comment this out when using index.php
    exit;
}
like image 109
EaterOfCode Avatar answered Sep 20 '22 14:09

EaterOfCode


After the cname is created on your customer's end, you need to let Apache know to listen for it.

So if you have a very simple virtual host setup like so

<VirtualHost *:80>
    ServerName user.mysaasapp.com
    DocumentRoot /var/www/user
</VirtualHost>

And your customer's cname is something like this

customdomain.com        IN      CNAME  user.mysaasapp.com

You need to add customdomain.com to your virtualhost entry, so it becomes

<VirtualHost *:80>
    ServerName user.mysaasapp.com
    ServerAlias customdomain.com
    DocumentRoot /var/www/user
</VirtualHost>

I've made a few assumptions here, but that's how I've done it in the past, excluding any typos of course.

like image 45
Jeff Busby Avatar answered Sep 22 '22 14:09

Jeff Busby