How to blogger/wordpress/tumlr allow users to mask url
Asked Answered
T

5

14

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!

Toner answered 28/7, 2012 at 18:33 Comment(4)
Hi, the following answers cant work. I do not have a folder called User. DocumentRoot /var/www/user Appreciated if anyone can enlighten me! Please be informed that i am using. mysaasapp.com/?user=user to user.mysaasapp.comToner
You can try to do 1. Save the user's custom domain in a domain-user mapping table. 2. then whenever user comes to a custom domain add a rule to check a page like usermapping.php. 3. In usermapping.php fetch the user mapping for that domain, and then display the content accordingly.Sympathizer
@EaterOfCorpses can't manage to get it to work. i think it is my sever vhost configuration. I create a wildcard * using cpanel subdomain feature. will that auto generate: <VirtualHost *:80> ServerName mysaasapp.com ServerAlias *.mysaasapp.com DocumentRoot /var/www/user </VirtualHost>Toner
You should have to try it, i dont know cPanel very well I got one cPanel site but dont have the credentials atm but I think it should work otherwise you need to add a parked domain with *.mysaasapp.comBedcover
B
4

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;
}
Bedcover answered 6/8, 2012 at 11:58 Comment(12)
Hi, thanks for replying. However i do not have this folder called /var/www/user... i have a user.php file insteadToner
Ok, I will fix it and give you some PHP code too because this is not the most easy situationBedcover
Hi, user need to point their custom domain to mysaassapp.com -> what you are suggesting here is a wildcard mask? in a nutshell, user need to create cname on their domain and point it to their user.mysaassapp.com.. so when they go to theircustomdomain.com, it will display user.mysaassapp.comToner
@Toner yes it will display the content of user.mysaasapp.com but the url will show theircustomdomain.comBedcover
where do the user need to change the cname? the above cname changed is only in my server right? doesnt seems to have a point theircustomdomain.com stepToner
@Toner Im gonna at some text its to big for commentsBedcover
@Toner okay :) when you have problems post it here :)Bedcover
hi @EaterOfCorpses, i checked my server configuration and found out that i have this preconfigured: *.mydomain.com. 14400 IN A 174.232.123.12 - It's not CNAME, will that matters?Toner
when pointing to a different DNS you need to use an CNAME but when you're pointing to a IP you need to use an A so if that IP is your server host it should be okay, btw I got the credentials of my cPanel website so I (if I dont forget it) will look into it tonightBedcover
@Toner just found out thanks to @lanzz that i look in the HTTP_HOST header what just shows the customdomain.com instead of user.mysaasapp.com so you need to map all the domains of those people and point it to the good direction, lemme edit the code!Bedcover
HI @EateOfCorpses, i would like to ask what will happen if i did not put the php code? is it necessary? i can't figure out where that php code is required... (Sorry, i am a beginner.. trying as hard to learn). What is the php code used for?Toner
If you dont use the php code it will show your homepage instead of the saas app for that domain and I dont think you want that, you can also do this with a .htaccess and direct it to a different folder just what you want ;)Bedcover
T
2

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.

Tentative answered 31/7, 2012 at 15:2 Comment(2)
Hi, thanks for replying. However i do not have this folder called /var/www/user... i have a user.php file insteadToner
@Slay, that is example code, you should change it to your needs.Tweet
A
2

Since you want to handle any hostname thrown at your site, you will need to have your site in your primary virtual host (the one appearing first in your configuration). The primary virtual host will handle all requests for hostnames that have no specific virtual host defined for their hostname.

In your code, your should inspect $_SERVER['HTTP_HOST'] and determine the relevant user from it. You will need to handle four distinct cases:

  1. The request is for your main domain (or www.yourmaindomain), in which case you have no relevant user
  2. The request is for a subdomain on your main domain (e.g. foouser.yourmaindomain), then you know the request is for foouser
  3. The request is for none of the above, but you have a user configured to use that hostname. You will need to maintain those domain-to-user assignments in your code, which is beyond the scope of this question.
  4. The request is for none of the above; somebody has pointed a hostname to your IP in error, or has pointed the wrong hostname, or the hostname is not yet assigned to a user, or somebody simply is accessing a wrong hostname on your IP for any reason. You will need to display some kind of not-found page for this case.

@EaterOfCorpses' solution seems to assume that you will be able to determine which hostname the user's CNAME record points to; this is seldom true and would depend heavily on the user's browser (i.e. the browser would need to include Host: <CNAME target> instead of Host: <original hostname> in the HTTP request, but browsers provide the original hostname used in the URL, which will still be the user's custom domain and not the username.yourdomain pointed by the CNAME record).

Alkalinity answered 2/11, 2012 at 15:44 Comment(0)
M
0

Assuming you are using Apache httpd (based on the mention of mod_rewrite) you would use the following httpd configuration (from http://httpd.apache.org/docs/2.2/vhosts/mass.html#simple):

A simple dynamic virtual hosts example for your httpd.conf

# get the server name from the Host: header
UseCanonicalName Off

# this log format can be split per-virtual-host based on the first field
LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/access_log vcommon

# include the server name in the filenames used to satisfy requests
VirtualDocumentRoot /www/hosts/%0/docs
VirtualScriptAlias /www/hosts/%0/cgi-bin

Then you would need your "saas app" to link (either symbolically or otherwise) their CNAME "saas.theirdomain.com" as a directory like this: "/www/hosts/saas.theirdomain.com/docs" based on the example above.

e.g.

ln -s /www/mysaasapp/user /www/hosts/saas.theirdomain.com

Where /www/mysaasapp/user is where your saas app lives.

Maestas answered 6/8, 2012 at 19:20 Comment(0)
M
0

If your users are using default port for your services then you don't have to worry about anything. Just tell your users to change CNAME(Alias) & A(Host) settings in DNS Manager.

@ --> will points to DOMAIN_NAME (if they want to mask theirdomain.com to your services).

saas --> will point to user.mysaasapp.com (if they want to mask saas.theirdomain.com to yous services).

In case you let them use your specific services using a specific port then you can add that in your VirtualHost setting

<VirtualHost *:[[PORT_NUMBER]]>    // set to 80 if no special ports required.
ServerName mysaasapp.com
ServerAlias user.mysaasapp.com    // Change [[user]] to custom user's alias.
DocumentRoot USER_FOLDER 
</VirtualHost>

I hope this will solve your Problem.

Melva answered 5/11, 2012 at 9:27 Comment(1)
Hi, what do i put in USER_FOLDER ? there is basically no user folder. i am using same folder for every user -> using get parameter to identity what content to display for what user as stated in my first post.Toner

© 2022 - 2024 — McMap. All rights reserved.