通配符子域
我知道之前已经有过一些关于此问题的线索,但我已经尝试了所有建议的内容(我能找到的),但到目前为止没有任何效果对我有用......
考虑到这一点,这就是我想要做的:
首先,我想允许用户发布页面并为他们每个人提供一个自己选择的子域(例如:user.example.com
)。 据我所知,最好的方法是使用 mod_rewrite 和 .htaccess 将 user.example.com
映射到 example.com/user
代码> - 这是正确的吗?
如果这是正确的,有人可以给我明确的指示如何做到这一点吗?
另外,我正在使用 MAMP 在本地进行所有开发,因此如果有人可以告诉我如何设置本地环境以相同的方式工作(我读过这更困难),我将不胜感激。 老实说,我一直在尝试一切都没有用,而且由于这是我第一次做这样的事情,我完全迷失了。
其中一些答案确实很有帮助,但对于我想要的系统来说,为每个用户手动添加子域并不是一个选择。 我真正要问的是如何即时执行此操作,并将 wildcard.example.com
重定向到 example.com/wildcard
—— Tumblr 的设置方式这是我想做的事情的完美例子。
I know there have been a few threads on this before, but I have tried absolutely everything suggested (that I could find) and nothing has worked for me thus far...
With that in mind, here is what I'm trying to do:
First, I want to allow users to publish pages and give them each a subdomain of their choice (ex: user.example.com
). From what I can gather, the best way to do this is to map user.example.com
to example.com/user
with mod_rewrite and .htaccess
- is that correct?
If that is correct, can somebody give me explicit instructions on how to do this?
Also, I am doing all of my development locally, using MAMP, so if somebody could tell me how to set up my local environment to work in the same manner (I've read this is more difficult), I would greatly appreciate it. Honestly, I have been trying a everything to no avail, and since this is my first time doing something like this, I am completely lost.
Some of these answers have been REALLY helpful, but for the system I have in mind, manually adding a subdomain for each user is not an option. What I'm really asking is how to do this on the fly, and redirect wildcard.example.com
to example.com/wildcard
-- the way Tumblr is set up is a perfect example of what I'd like to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
至于如何设置 DNS 子域通配符,这将由您的 DNS 托管提供商负责。 这将是不同的步骤,具体取决于您拥有的托管提供商,并且对他们来说是一个更好的问题。
一旦你使用 DNS 主机进行了设置,从你的 Web 应用程序中你实际上只是 URL 重写,这可以通过 Web 服务器本身的某种模块来完成,例如 isapi 重写,如果你在 IIS 上(这如果可能的话,将是首选路线)。 您还可以在应用程序级别处理重写(例如在 ASP.NET 上使用路由)。
您需要重写 URL,使
http://myname.example.com
变为http://example.com/something.aspx?name=myname
或其他内容。 从那时起,您只需像平常一样处理 myname 值在查询字符串中即可。 那有意义吗? 希望我没有误解你的目的。我并不是建议您为每个用户创建一个子域,而是为域本身创建一个通配符子域,因此 anything.example.com (基本上是
*.example.com
) 转到您的网站。 我使用 MyDomain 设置了多个域。 他们的设置说明如下:我有一些是以这种方式设置的,让
*.example.com
转到我的网站。 然后,我可以读取 Web 应用程序中的基本 URL,以查看当前访问的内容是ryan.example.com
,或者使用的内容是bill.example.com
。 然后,我可以:那有意义吗? 我有几个站点以这种确切的方式设置:使用 DNS 主机为域创建通配符,然后简单地从 URL 中读取主机或基域,以决定根据子域(实际上是用户名)显示什么内容)
编辑 2:
如果没有 DNS 条目,则无法执行此操作。 “在线世界”需要知道
name1.example.com
、name2.example.com
、...、nameN.example.com
全部转到您服务器的 IP 地址。 执行此操作的唯一方法是使用适当的 DNS 条目。 您必须使用 DNS 主机为您的域添加通配符 DNS 条目。 然后,您只需从 URL 中读取子域并在代码中采取适当的操作即可。As far as how to set up the DNS subdomain wildcard, that would be a function of your DNS hosting provider. This would be different steps depending on which hosting provider you have and would be a better question for them.
Once you've set that up with the DNS host, from your web app you really are just URL rewriting, which can be done with some sort of module for the web server itself, such as isapi rewrite if you're on IIS (this would be the preferred route if possible). You could also handle rewriting at the application level as well (like using routing if on ASP.NET).
You'd rewrite the URL so
http://myname.example.com
would becomehttp://example.com/something.aspx?name=myname
or something. From there on out, you just handle it as if the myname value was in the query string as normal. Does that make sense? Hope I didn't misunderstand what you're after.I am not suggesting that you create a subdomain for each user, but instead create a wildcard subdomain for the domain itself, so anything.example.com (basically
*.example.com
) goes to your site. I have several domains setup with MyDomain. Their instructions for setting this up is like this:I have some that are setup in just this way, having
*.example.com
go to my site. I then can read the base URL in my web app to see thatryan.example.com
is what was currently accessed, or thatbill.example.com
is what was used. I can then either:Does that make sense? I have several sites set up in just this exact way: create the wildcard for the domain with the DNS host and then simply read the host, or base domain from the URL to decide what to display based on the subdomain (which was actually a username)
Edit 2:
There is no way to do this without a DNS entry. The "online world" needs to know that
name1.example.com
,name2.example.com
,...,nameN.example.com
all go to the IP address for your server. The only way to do this is with the appropriate DNS entry. You have to add the wildcard DNS entry for your domain with your DNS host. Then it's just a matter of you reading the subdomain from the URL and taking the appropriate action in your code.如果您正在运行 *AMP,最好的做法是按照 Thomas 的建议,在 Apache 中创建虚拟主机。 您可以使用或不使用您描述的重定向来执行此操作。
虚拟主机
您很可能会想要基于名称的虚拟主机 ,因为它最容易设置并且只需要一个 IP 地址(因此也很容易在本地 MAMP 计算机上设置和测试)。 基于 IP 的虚拟主机在其他一些方面更好,但每个域都必须有一个 IP 地址。
此维基百科页面讨论了差异以及指向如何命名的良好基本演练的链接基于虚拟主机的底部。
在本地计算机上进行测试时,您还必须在
/etc/hosts
中为假测试域名设置假 DNS 名称。 即,如果您在 localhost 上监听 Apache 并在 Apache 配置中设置 vhost1.test.domain 和 vhost2.test.domain,则只需将这些域添加到127.0.0.1
行即可>/etc/hosts,位于 localhost 之后:完成
/etc/hosts
编辑并将基于名称的虚拟主机配置添加到 Apache 配置文件后,即重启 Apache,您的测试域就应该可以工作了。使用 mod_rewrite 重定向
如果您想使用 mod_rewrite 进行重定向(以便
user.example.com
不直接托管,而是重定向到example.com/user
),那么您还需要执行 RewriteCond 来匹配子域并重定向它:您可以将其放入
.htaccess
或主 Apache 配置中。您需要为要重定向的每个子域添加一对规则,例如最后两个规则。 或者,您也许能够在 RewriteCond 中捕获子域,以便能够使用一个通配符规则将 *.example.com 重定向到
example.com/
总之,虚拟主机和重定向
最好更明确地为每个要侦听的主机名设置一个虚拟主机配置部分,并将每个主机名的重写规则放入其虚拟主机配置中。 (如果可以的话,将此类内容放入 Apache 配置中而不是
.htaccess
总是更安全、更快速 -.htaccess
会降低性能,因为 Apache不断地在文件系统中搜索 .htaccess 文件并重新解析它们,而且它的安全性较低,因为这些文件可能会被用户搞砸。)像这样,您的 Apache 配置中的 vhost 配置将是:
The best thing to do if you are running *AMP is to do what Thomas suggests and do virtual hosts in Apache. You can do this either with or without the redirect you describe.
Virtual hosts
Most likely you will want to do name-based virtual hosts, as it's easiest to set up and only requires one IP address (so will also be easy to set up and test on your local MAMP machine). IP-based virtual hosts is better in some other respects, but you have to have an IP address for each domain.
This Wikipedia page discusses the differences and links to a good basic walk-thru of how to do name-based vhosts at the bottom.
On your local machine for testing, you'll also have to set up fake DNS names in
/etc/hosts
for your fake test domain names. i.e. if you have Apache listening on localhost and set up vhost1.test.domain and vhost2.test.domain in your Apache configs, you'd just add these domains to the127.0.0.1
line in/etc/hosts
, after localhost:Once you've done the
/etc/hosts
edit and added the name-based virtual host configs to your Apache configuration file(s), that's it, restart Apache and your test domains should work.Redirect with mod_rewrite
If you want to do redirects with mod_rewrite (so that
user.example.com
isn't directly hosted and instead redirects toexample.com/user
), then you will also need to do a RewriteCond to match the subdomain and redirect it:You can put this in a
.htaccess
or in your main Apache config.You will need to add a pair of rules like the last two for each subdomain you want to redirect. Or, you may be able to capture the subdomain in a RewriteCond to be able to use one wildcard rule to redirect *.example.com to
example.com/
All together, vhosts and redirect
It's better to be more explicit and set up a virtual host configuration section for each hostname you want to listen for, and put the rewrite rules for each of these hostnames inside its virtual host config. (It is always more secure and faster to put this kind of stuff inside your Apache config and not
.htaccess
, if you can help it --.htaccess
slows performance because Apache is constantly scouring the filesystem for.htaccess
files and reparsing them, and it's less secure because these can be screwed up by users.)All together like that, the vhost config inside your Apache configs would be:
我为 Ubuntu 18.04 找到的解决方案与 这个 类似,但涉及 NetworkManager 配置:
编辑文件
/etc/NetworkManager/NetworkManager.conf
,并将行dns=dnsmasq
添加到[main]
部分应该看起来像这样:
<前><代码>[主要]
插件=ifupdown,密钥文件
dns=dnsmasq
...
开始使用 NetworkManager 的 resolv.conf
使用通配符配置创建文件
重新加载 NetworkManager 配置
测试它
您还可以添加任何其他域,这对于使用本地开发设置时的某些类型的身份验证非常有用。
然后这有效:
The solution I found for Ubuntu 18.04 is similar to this one but involves NetworkManager config:
Edit the file
/etc/NetworkManager/NetworkManager.conf
, and add the linedns=dnsmasq
to the[main]
sectionshould look like this:
Start using NetworkManager's resolv.conf
Create a file with your wildcard configuration
Reload NetworkManager configuration
Test it
You can also add any other domain, quite useful for some types of authentication when using a local development setup.
Then this works:
我必须对我的一个网站做同样的事情。 您可以按照以下步骤操作
如果您的服务器上有cPanel,请创建一个子域
*
,如果没有,您必须在DNS中设置A记录(用于BIND请参阅 http://ma.tt/2003/10/wildcard- dns 和子域/)。 在你的开发上。 如果您的服务器上有 cPanel ,您最好通过将每个子域添加到您的hosts
文件中来伪造子域。(如果您使用 cPanel,则无需执行此操作)。 您必须将如下内容添加到您的 apache vhosts 文件中。 这很大程度上取决于您运行的服务器类型(共享或非共享)。 以下代码不完整。 这只是为了提供方向。 注意:
ServerAlias example.com *.example.com
很重要。下一步,因为您可以使用 PHP 脚本检查“Host”标头并找出子域并相应地提供内容。
I had to do exactly the same for one of my sites. You can follow the following steps
If you've cPanel on your server, create a subdomain
*
, if not, you'd have to set-up an A record in your DNS (for BIND see http://ma.tt/2003/10/wildcard-dns-and-sub-domains/). On your dev. server you'd be far better off faking subdomains by adding each to yourhosts
file.(If you used cPanel you won't have to do this). You'll have to add soemthing like the following to your apache vhosts file. It largely depends on what type of server (shared or not) you're running. THE FOLLOWING CODE IS NOT COMPLETE. IT'S JUST TO GIVE DIRECTION. NOTE:
ServerAlias example.com *.example.com
is important.Next, since you can use the PHP script to check the "Host" header and find out the subdomain and serve content accordingly.
使用虚拟主机可能会更好。 这样,每个用户都可以拥有一个几乎独立于其他用户的网络服务器配置。
语法是这样的:
You may be better off using virtual hosts. That way, each user can have a webserver configuration pretty much independent of others.
The syntax goes something like this:
从我在许多网络主机上看到的情况来看,他们在 apache 上设置了虚拟主机。
因此,如果您的 www.mysite.com 由 /var/www 提供服务,您可以为每个用户创建一个文件夹。 然后将虚拟主机映射到该文件夹。
这样,mysite.com/user 和 user.mysite.com 都可以工作。
至于您的测试环境,如果您使用的是 Windows,我建议编辑您的 HOSTS 文件,将 mysite.com 映射到您的本地 PC (127.0.0.1),以及您为测试设置的任何子域。
From what I have seen on many webhosts, they setup a virtual host on apache.
So if your www.mysite.com is served from /var/www, you could create a folder for each user. Then map the virtual host to that folder.
With that, both mysite.com/user and user.mysite.com works.
As for your test enviroment, if you are on windows, I would suggest editing your HOSTS file to map mysite.com to your local PC (127.0.0.1), as well as any subdomains you set up for testing.
我使用的是 Ubuntu 16.04,从 14.04 开始,我使用 Dave Evans 提供的解决方案 在这里,它对我来说效果很好。
安装
dnsmasq
在
/etc/dnsmasq.d
目录下创建新文件localhost.conf
以下行编辑
/etc/ dhcp/dhclient.conf
并添加以下行(您可能会发现该行已经存在,您只需取消注释即可。)
最后一个是重新启动服务
最后,您应该检查它是否正常工作。
注意:
如果您想在网络服务器上使用它,只需将
127.0.0.0
更改为您的实际IP地址即可。I am on Ubuntu 16.04 and since 14.04 I've using solution provided by Dave Evans here and it works fine for me.
Install
dnsmasq
Create new file
localhost.conf
under/etc/dnsmasq.d
dir with the following lineEdit
/etc/dhcp/dhclient.conf
and add the following line(You’ll probably find that this line is already there and you just need to uncomment it.)
Last one is restart the service
Finally, you should check if it's working.
note:
If you want to use it on your web server, you need to simply change the
127.0.0.0
to your actual IP address.我意识到我回答这个问题已经很晚了,但我在本地开发解决方案方面遇到了同样的问题。 在另一个SO线程中我发现更好的解决方案,并认为我将来会与任何有相同问题的人分享:
VMware 拥有的通配符域将任何子域解析为 127.0.0.1:
或为了获得更多功能 37 Signals 拥有一个域,可以使用以下命令将任何子域映射到任何给定的 IP:具体格式:
请参阅 xip.io 了解更多信息
I realize that I'm pretty late responding to this question, but I had the same problem in regards to a local development solution. In another SO thread I found better solutions and thought I would share them for anyone with the same question in the future:
VMware owned wild card domain that resolves any subdomain to 127.0.0.1:
or for more versatility 37 Signals owns a domain to map any subdomain to any given IP using a specific format:
see xip.io for more info