Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php blogger/wordpress/tumlr如何允许用户屏蔽url_Php_.htaccess_Mod Rewrite_Cross Domain_Cname - Fatal编程技术网

Php blogger/wordpress/tumlr如何允许用户屏蔽url

Php blogger/wordpress/tumlr如何允许用户屏蔽url,php,.htaccess,mod-rewrite,cross-domain,cname,Php,.htaccess,Mod Rewrite,Cross Domain,Cname,我正在创建一个saas应用程序。用户有自己的url,比如user.mysasaapp.com 为了允许用户拥有自己的url,我使用mod rewrite。类似于http://mysaasapp.com/?user=user至user.mysasaapp.com(此功能运行良好) 现在,我想让用户能够灵活地屏蔽他们的url。这意味着用户可以将其子域(saas.theirdomain.com)指向user.mysasasapp.com。我该怎么做呢 我读了很多文章(cname),但仍然没有一个明确的

我正在创建一个
saas
应用程序。用户有自己的url,比如user.mysasaapp.com

为了允许用户拥有自己的url,我使用mod rewrite。类似于
http://mysaasapp.com/?user=user
user.mysasaapp.com
(此功能运行良好)

现在,我想让用户能够灵活地屏蔽他们的url。这意味着用户可以将其子域(saas.theirdomain.com)指向user.mysasasapp.com。我该怎么做呢

我读了很多文章(cname),但仍然没有一个明确的解决方案。我想知道像wordpress/google这样的大公司是如何做到这一点的

e、 g

e、 g

我意识到公司会采取以下步骤:

  • 让用户添加cname
  • 让用户在saas应用程序中输入其域(可能在自定义域下)

  • 我需要做什么才能让它工作?谢谢

    在客户端创建cname后,您需要让Apache知道如何监听它

    因此,如果您有一个非常简单的虚拟主机设置,如

    <VirtualHost *:80>
        ServerName user.mysaasapp.com
        DocumentRoot /var/www/user
    </VirtualHost>
    
    您需要将customdomain.com添加到virtualhost条目中,这样它就变得

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

    我在这里做了一些假设,但我过去就是这样做的,当然不包括任何打字错误。

    如果我是对的,你可以在
    ServerAlias
    中使用通配符,通过mod_rewrite,你可以将它重写到正确的文件中,这样你的vhost就会

    <VirtualHost *:80>
        ServerName mysaasapp.com
        ServerAlias *.mysaasapp.com
        DocumentRoot /var/www/user
    </VirtualHost>
    
    并将
    [STANDARDFOLDER]
    替换为myaasapp.com的
    文档根目录

    在您的
    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
    }
    
    这是解决你问题的一个非常糟糕的办法 我希望它现在对你有用

    更新:

    起初我不知道你还想从自定义域重定向

    然后,他们必须使用

    theirdomain.com IN CNAME thierusername.mysaasapp.com
    
    如果你的DNS记录是这样的,那么所有的子域都将进入你的apache,那么它应该使用我给你的代码和vhost吗

    更新2:

    lanzz刚刚指出了一些我忘记的东西(facepalm)我正在检查主机头什么是没有意义的,因为它将有
    customdomain.com
    ,所以你需要询问你的用户他们的域(如果你没有共享IP,让他们用CNAME指向,如果你没有共享IP,你可以让他们用a记录指向你的IP)并从HTTP_主机中查找域,然后在数据库中搜索该域,然后它就可以工作了

    一点点经过编辑的代码:

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

    假设您使用的是Apache httpd(基于mod_rewrite的说法),那么您将使用以下httpd配置(从):

    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
    
    然后,您需要您的“saas应用程序”将其CNAME“saas.theirdomain.com”链接(无论是象征性的还是其他的)为如下目录:“/www/hosts/saas.theirdomain.com/docs”,基于上述示例

    e、 g


    其中/www/mysasapp/user是您的saas应用程序所在的位置。

    由于您希望处理在站点上抛出的任何主机名,因此您需要将站点放在主虚拟主机()中。主虚拟主机将处理没有为主机名定义特定虚拟主机的所有主机名请求

    在代码中,您应该检查
    $\u服务器['HTTP\u HOST']
    ,并从中确定相关用户。您需要处理四种不同的情况:

  • 该请求针对您的主域(或
    www.yourmaindomain
    ),在这种情况下,您没有相关的用户
  • 请求是针对主域上的子域(例如,
    fooouser.yourmaindomain
    ),那么您就知道请求是针对
    fooouser
  • 请求不针对上述任何一项,但您已将用户配置为使用该主机名。您需要在代码中维护这些域到用户的分配,这超出了本问题的范围
  • 该请求不适用于上述任何一项;有人错误地将主机名指向了您的IP,或者指出了错误的主机名,或者主机名尚未分配给用户,或者有人只是出于任何原因访问了您IP上的错误主机名。对于这种情况,您需要显示某种未找到的页面

  • @EaterofCorbites的解决方案似乎假设您将能够确定用户的
    CNAME
    记录指向哪个主机名;这很少是真的,而且在很大程度上取决于用户的浏览器(即,浏览器需要在HTTP请求中包含
    Host:
    而不是
    Host:
    ,但浏览器提供URL中使用的原始主机名,该主机名仍然是用户的自定义域,而不是
    CNAME
    记录所指向的
    用户名。yourdomain
    ).

    如果您的用户正在为您的服务使用默认端口,则您无需担心任何问题。只需告诉您的用户在DNS管理器中更改CNAME(别名)和A(主机)设置即可

    @-->将指向域名(如果他们想屏蔽 theirdomain.com连接到您的服务)

    saas-->将指向user.mysasasapp.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>
    
    //如果不需要特殊端口,则设置为80。
    ServerName mysasaapp.com
    ServerAlias user.mysasaapp.com//将[[user]]更改为自定义用户的别名。
    DocumentRoot用户文件夹
    

    我希望这能解决你的问题。

    嗨,谢谢你的回复。但是我没有这个名为/var/www/user的文件夹…我有一个user.php文件,好的,我会修复它,并给你一些php代码,因为这不是最简单的情况。嗨,用户需要将他们的自定义域指向myaassapp.com->你在这里建议的是一个wil
    # 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
    
    ln -s /www/mysaasapp/user /www/hosts/saas.theirdomain.com
    
    <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>