14

アプリケーションを作成していsaasます。ユーザーは、 user.mysaasapp.comのような独自のURLを持っています

ユーザーが独自のURLを持つことができるようにするために、私はmodrewriteを使用します。のようなhttp://mysaasapp.com/?user=userものuser.mysaasapp.com(これは完全に機能しています)

ここで、ユーザーにURLをマスクする柔軟性を提供したいと思います。これは、ユーザーがサブドメイン(saas.theirdomain.com)をuser.mysaasapp.comに転送できることを意味します。どうすればそうすることができますか。

私はこれに関する多くの記事(cname)を読みましたが、まだ明確な解決策がありません。私はwordpress/googleのような大企業がそれをどのように行っているのか疑問に思っています。

例:http ://www.tumblr.com/docs/en/custom_domains

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

私は、企業がこのプロセスをとることを理解しています。

  1. ユーザーにcnameを追加してもらう
  2. ユーザーにsaasアプリにドメインを入力してもらいます(おそらくカスタムドメインの下にあります)

これを機能させるために私から何が必要ですか?ありがとう!

4

5 に答える 5

4

私が正しければ、ワイルドカードを使用ServerAliasして mod_rewrite を使用すると、適切なファイルに書き換えて、仮想ホストが次のようになります

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

あなたのCNAMEは次のようになります

*.mysaasapp.com IN CNAME mysaasapp.com

これが役立つことを願っています

編集:

仮想ホストをに変更します

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

mysaasapp.com[STANDARDFOLDER]のに置き換えます。DocumentRoot

index.phpそして一番上のあなたの場所にこれ:

// 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
}

これはあなたの問題に対する本当に汚い解決策です。今はうまくいくことを願っています

アップデート:

最初は、あなたがカスタム ドメインからもリダイレクトすることを望んでいるとは知りませんでした。

次に、DNSサーバー/マネージャーにCNAMEを追加する必要があります

theirdomain.com IN CNAME thierusername.mysaasapp.com

DNSレコードがすべてのサブドメインがApacheに送信されるようになっている場合、Apacheはコードと私が提供したVhostsで動作する必要があります

更新 2:

lanzz は私が忘れていたことを指摘しました (facepalm) HOST ヘッダーをチェックしていますが、意味がないcustomdomain.comので、ユーザーにドメインを尋ねる必要があります (共有 IP を持っていない場合は、CNAME を指定させてください。共有 IP を持っていない場合は、A レコードを使用して IP を指すようにすることができます)、HTTP_HOST からドメインを検索し、データベースでそれを検索すると、動作するはずです!

少し編集したコード:

//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;
}
于 2012-08-06T11:58:23.297 に答える
2

顧客側で cname が作成されたら、それをリッスンするように Apache に知らせる必要があります。

したがって、そのような非常に単純な仮想ホストのセットアップがある場合

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

そして、あなたの顧客のcnameはこのようなものです

customdomain.com        IN      CNAME  user.mysaasapp.com

virtualhost エントリに customdomain.com を追加する必要があるため、次のようになります。

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

ここでいくつかの仮定を立てましたが、もちろんタイプミスを除いて、これが過去に行った方法です。

于 2012-07-31T15:02:57.963 に答える
2

サイトでスローされたホスト名を処理する必要があるため、プライマリ仮想ホスト (構成で最初に表示されるホスト) にサイトを配置する必要があります。プライマリ仮想ホストは、ホスト名に対して特定の仮想ホストが定義されていないホスト名に対するすべての要求を処理します。

コードでは、$_SERVER['HTTP_HOST']そこから関連するユーザーを調べて判断する必要があります。次の 4 つのケースを処理する必要があります。

  1. リクエストはメイン ドメイン (またはwww.yourmaindomain) に対するものです。この場合、関連するユーザーはいません。
  2. リクエストがメイン ドメインのサブドメインに対するものである場合 (例: foouser.yourmaindomain)、リクエストがfoouser
  3. リクエストは上記のいずれでもありませんが、そのホスト名を使用するように構成されたユーザーがいます。これらのドメインからユーザーへの割り当てをコードで維持する必要がありますが、これはこの質問の範囲を超えています。
  4. 要求は上記のいずれにも当てはまりません。誰かが誤ってあなたの IP にホスト名を指定したか、間違ったホスト名を指定したか、ホスト名がまだユーザーに割り当てられていないか、何らかの理由で誰かが単にあなたの IP 上の間違ったホスト名にアクセスしています。この場合、何らかの種類の見つからないページを表示する必要があります。

@ EaterOfCorpses のソリューションは、ユーザーのレコードが指しているホスト名を特定できると想定しているようです。CNAMEこれはめったに当てはまらず、ユーザーのブラウザーに大きく依存します (つまり、ブラウザーはHTTP 要求のHost: <CNAME target>代わりに含める必要がありますが、ブラウザーはHost: <original hostname>URL で使用される元のホスト名を提供します。記録username.yourdomainによるCNAME)。

于 2012-11-02T15:44:55.960 に答える
0

Apache httpd を使用していると仮定すると (mod_rewrite の言及に基づいて)、次の httpd 構成を使用します ( http://httpd.apache.org/docs/2.2/vhosts/mass.html#simpleから):

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

次に、例に基づいて「/www/hosts/saas.theirdomain.com/docs」のようなディレクトリとしてCNAME「saas.theirdomain.com」をリンクする「saasアプリ」が必要です。その上。

例えば

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

/www/mysaasapp/user は、SaaS アプリが存在する場所です。

于 2012-08-06T19:20:18.443 に答える
0

ユーザーがサービスのデフォルト ポートを使用している場合は、何も心配する必要はありません。DNS Manager で CNAME(Alias) と A(Host) の設定を変更するようにユーザーに指示するだけです。

@ --> は DOMAIN_NAME を指します (彼らが自分のドメインをあなたのサービスにマスクしたい場合)。

saas --> user.mysaasapp.com を指します (saas.theirdomain.com をあなたのサービスにマスクしたい場合)。

特定のポートを使用して特定のサービスを使用できるようにする場合は、それを VirtualHost 設定に追加できます。

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

これで問題が解決することを願っています。

于 2012-11-05T09:27:50.483 に答える