PHP Laravel - 允许用户使用自定义域名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24327868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:40:36  来源:igfitidea点击:

PHP Laravel - Allow users to use a custom domain name

phpapachelaraveldns

提问by ericbae

I've looked through pretty much every single page I could find on the topic, but I'm still bit confused.

我已经浏览了几乎可以找到的关于该主题的每一页,但我仍然有点困惑。

I have a PHP Laravel 4.0 web site, which is at

我有一个 PHP Laravel 4.0 网站,位于

http://www.mywebsite.com

http://www.mywebsite.com

When a user signs up, each user gets their own page

当用户注册时,每个用户都有自己的页面

http://user1.mywebsite.com

http://user1.mywebsite.com

I have set the above via wildcard sub domain on my VPS DNS manager (Linode).

我已经在我的 VPS DNS 管理器 (Linode) 上通过通配符子域设置了上述内容。

And in my Laravel, I have a domain route that picks up the subdomain and displayes the correct user's page. All works well.

在我的 Laravel 中,我有一个域路由,可以选择子域并显示正确的用户页面。一切正常。

Now, what I want to achieve is have a user use their own domain name and point to their page on my website.

现在,我想要实现的是让用户使用他们自己的域名并指向他们在我网站上的页面。

So if a user signs up with

所以如果用户注册

http://www.user1domain.com

http://www.user1domain.com

I'd like this domain to point to http://user1.mywebsite.com

我希望这个域指向http://user1.mywebsite.com

I do not want a redirect, but the URL to remain at http://www.user1domain.com(obviously).

我不想重定向,但 URL 保留在http://www.user1domain.com(显然)。

I know that sites like Tumblr allows this.

我知道像 Tumblr 这样的网站允许这样做。

What steps do I take in achieving this? Here are somethings I thought of during my research.

我将采取哪些步骤来实现这一目标?以下是我在研究过程中想到的一些事情。

  1. Have the user to make their custom domain "user1domain.com" to point to my VPS name server (easy).

  2. On my end (here's where I fall short), do I

    • Create a new DNS entry? (In my Linode VPS, I would do this in "DNS Manager" and "Add a domain zone")

    • I'm using Apache, so do I have to create a VirtualHost (like I do for all of my other sites), does this mean I have to create a new VirtualHost file for every user who wants to use a custom domain?

    • If NOT VirtualHost, how do I actually point to my web application folder? Apache alias? (How do I set that up?)

  3. Assuming the user's domain name now "points" to my web application, how do I now pick that up in Laravel? (I'm guessing I'd do this the same way I do for my sub domains)

  1. 让用户让他们的自定义域“user1domain.com”指向我的 VPS 名称服务器(简单)。

  2. 在我的最后(这是我的不足之处),我是不是

    • 创建一个新的 DNS 条目?(在我的 Linode VPS 中,我会在“DNS 管理器”和“添加域区域”中执行此操作)

    • 我正在使用 Apache,所以我是否必须创建一个 VirtualHost(就像我为所有其他站点所做的那样),这是否意味着我必须为每个想要使用自定义域的用户创建一个新的 VirtualHost 文件?

    • 如果不是 VirtualHost,我如何实际指向我的 Web 应用程序文件夹?阿帕奇别名?(我该如何设置?)

  3. 假设用户的域名现在“指向”我的 Web 应用程序,我现在如何在 Laravel 中获取它?(我猜我会像处理子域一样这样做)

Thanks for your time.

谢谢你的时间。

回答by Unnawut

Question: Create a new DNS entry? (In my Linode VPS, I would do this in "DNS Manager" and "Add a domain zone")

问题:创建一个新的 DNS 条目?(在我的 Linode VPS 中,我会在“DNS 管理器”和“添加域区域”中执行此操作)

Answer: Yes, you'll need to ask your user to point their domain to your nameserver. You'll also have to add their domains into your DNS Manager.

答:是的,您需要让您的用户将他们的域指向您的域名服务器。您还必须将他们的域添加到您的 DNS 管理器中。

Question: I'm using Apache, so do I have to create a VirtualHost (like I do for all of my other sites), does this mean I have to create a new VirtualHost file for every user who wants to use a custom domain?

If NOT VirtualHost, how do I actually point to my web application folder? Apache alias? (How do I set that up?)

问题:我使用的是 Apache,所以我是否必须创建一个 VirtualHost(就像我为所有其他站点所做的那样),这是否意味着我必须为每个想要使用自定义域的用户创建一个新的 VirtualHost 文件?

如果不是 VirtualHost,我如何实际指向我的 Web 应用程序文件夹?阿帕奇别名?(我该如何设置?)

Answer: You can set up just one virtual host configuration for all, as documented in Using defaultvhosts.

答:您可以为所有主机设置一个虚拟主机配置,如使用默认虚拟主机中所述

<VirtualHost _default_:80>
    DocumentRoot /var/www/mysharedsite
    # ...
</VirtualHost>

So all domain names that are requesting to your server will be served by your only one code base.

因此,向您的服务器请求的所有域名都将由您唯一的一个代码库提供服务。

Question: Assuming the user's domain name now "points" to my web application, how do I now pick that up in Laravel? (I'm guessing I'd do this the same way I do for my sub domains)

问题:假设用户的域名现在“指向”我的 Web 应用程序,我现在如何在 Laravel 中获取它?(我猜我会像处理子域一样这样做)

I can think of two ways right now. Yes you could adapt Laravel's documentation on Sub-Domain Routing, but use for different domains instead:

我现在能想到两种方法。是的,您可以修改 Laravel 关于子域路由的文档,但用于不同的域:

Route::group(array('domain' => 'user1domain.com'), function()
{
    Route::get('blog/{id}', 'BlogController@view');
});

But I suppose that functionalities will be quite the same between different domains, it's only the data that changes, you could use always use:

但是我认为不同域之间的功能将完全相同,只是数据发生了变化,您可以始终使用:

Request::getHost();

So you get the domain, and perhaps map it to a website_id column in your database to get the correct content.

因此,您获得了域,并且可能将其映射到数据库中的 website_id 列以获取正确的内容。

Make sure you whitelist the acceptable domain names carefully in your project. Letting all domain names in isn't a good idea. You could limit by using ServerNameand ServerAliasin your virtual host as well, but I guess that limits the flexibility of letting new users have their own domains.

确保在项目中小心地将可接受的域名列入白名单。让所有域名都进来并不是一个好主意。您也可以通过在虚拟主机中使用ServerNameServerAlias进行限制,但我想这限制了让新用户拥有自己的域的灵活性。

回答by Mubin

Basicly for created dynamic wildcard you just need this..

基本上对于创建的动态通配符,您只需要这个..

  1. setup your wildcard on domain (A Record) to your server ip address

    *.mywebsite.com IN A 192.168.1.1
    
  2. Setup Your servername & serveralias Apache Conf for domain name inside VirtualHost

    ServerName mywebsite.com
    
    ServerAlias mywebsite.com *.mywebsite.com 
    
  3. clear dnscache on window console

    ipconfig/flushdns 
    
  1. 将域(A 记录)上的通配符设置为您的服务器 IP 地址

    *.mywebsite.com IN A 192.168.1.1
    
  2. 在 VirtualHost 中为域名设置您的服务器名称和服务器别名 Apache Conf

    ServerName mywebsite.com
    
    ServerAlias mywebsite.com *.mywebsite.com 
    
  3. 清除窗口控制台上的 dnscache

    ipconfig/flushdns 
    

hope this help

希望这有帮助