通配符子域

发布于 2024-07-05 13:31:08 字数 630 浏览 6 评论 0原文

我知道之前已经有过一些关于此问题的线索,但我已经尝试了所有建议的内容(我能找到的),但到目前为止没有任何效果对我有用......

考虑到这一点,这就是我想要做的:

首先,我想允许用户发布页面并为他们每个人提供一个自己选择的子域(例如: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

安人多梦 2024-07-12 13:31:08

至于如何设置 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 设置了多个域。 他们的设置说明如下:

是的,您可以配置通配符,但是
仅当您将其设置为
A 记录。 通配符不起作用
具有 C 名称。 要使用通配符,您
使用星号字符“”。 为了
例如,如果您创建 A 记录
使用通配符,*.example.com
在该地方输入的任何内容
'
' 所在的位置将解析
到指定的IP地址。 所以如果你
输入“www”、“ftp”、“站点”或
域名之前的任何其他内容,
它将始终解析为 IP
地址

我有一些是以这种方式设置的,让 *.example.com 转到我的网站。 然后,我可以读取 Web 应用程序中的基本 URL,以查看当前访问的内容是 ryan.example.com,或者使用的内容是 bill.example.com 。 然后,我可以:

  1. 使用 URL 重写,使子域成为查询字符串的一部分,或者
  2. 简单地从访问的 URL 中读取主机值,并根据该值执行一些逻辑。

那有意义吗? 我有几个站点以这种确切的方式设置:使用 DNS 主机为域创建通配符,然后简单地从 URL 中读取主机或基域,以决定根据子域(实际上是用户名)显示什么内容)

编辑 2:

如果没有 DNS 条目,则无法执行此操作。 “在线世界”需要知道 name1.example.comname2.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 become http://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:

Yes, you can configure a wild card but
it will only work if you set it up as
an A Record. Wildcards do not work
with a C Name. To use a wildcard, you
use the asterisks character ''. For
example, if you create and A Record
using a wild card, *.example.com,
anything that is entered in the place
where the '
' is located, will resolve
to the specified IP address. So if you
enter 'www', 'ftp', 'site', or
anything else before the domain name,
it will always resolve to the IP
address

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 that ryan.example.com is what was currently accessed, or that bill.example.com is what was used. I can then either:

  1. Use URL rewriting so that the subdomain becomes a part of the query string OR
  2. Simply read the host value from the accessed URL and perform some logic based on that value.

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.

桜花祭 2024-07-12 13:31:08

如果您正在运行 *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 之后:

127.0.0.1 localhost vhost1.test.domain vhost2.test.domain

完成 /etc/hosts 编辑并将基于名称的虚拟主机配置添加到 Apache 配置文件后,即重启 Apache,您的测试域就应该可以工作了。

使用 mod_rewrite 重定向

如果您想使用 mod_rewrite 进行重定向(以便 user.example.com 不直接托管,而是重定向到 example.com/user),那么您还需要执行 RewriteCond 来匹配子域并重定向它:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com
RewriteRule ^(.*)$ http://example.com/subdomain$1 [R]

您可以将其放入 .htaccess 或主 Apache 配置中。

您需要为要重定向的每个子域添加一对规则,例如最后两个规则。 或者,您也许能够在 RewriteCond 中捕获子域,以便能够使用一个通配符规则将 *.example.com 重定向到 example.com/

  • ——但这对我来说真的很糟糕安全的角度。

总之,虚拟主机和重定向

最好更明确地为每个要侦听的主机名设置一个虚拟主机配置部分,并将每个主机名的重写规则放入其虚拟主机配置中。 (如果可以的话,将此类内容放入 Apache 配置中而不是 .htaccess 总是更安全、更快速 - .htaccess 会降低性能,因为 Apache不断地在文件系统中搜索 .htaccess 文件并重新解析它们,而且它的安全性较低,因为这些文件可能会被用户搞砸。)

像这样,您的 Apache 配置中的 vhost 配置将是:

NameVirtualHost 127.0.0.1:80

# Your "default" configuration must go first
<VirtualHost 127.0.0.1:80>
  ServerName example.com
  ServerAlias www.example.com
  DocumentRoot /www/siteroot
  # etc.
</VirtualHost>

# First subdomain you want to redirect
<VirtualHost 127.0.0.1:80>
  ServerName vhost1.example.com
  RewriteEngine On
  RewriteRule ^(.*)$ http://example.com/vhost1$1 [R]
</VirtualHost>

# Second subdomain you want to redirect
<VirtualHost 127.0.0.1:80>
  ServerName vhost2.example.com
  RewriteEngine On
  RewriteRule ^(.*)$ http://example.com/vhost2$1 [R]
</VirtualHost>

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 the 127.0.0.1 line in /etc/hosts, after localhost:

127.0.0.1 localhost vhost1.test.domain vhost2.test.domain

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 to example.com/user), then you will also need to do a RewriteCond to match the subdomain and redirect it:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com
RewriteRule ^(.*)$ http://example.com/subdomain$1 [R]

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/

  • -- but that smells really bad to me from a security standpoint.

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:

NameVirtualHost 127.0.0.1:80

# Your "default" configuration must go first
<VirtualHost 127.0.0.1:80>
  ServerName example.com
  ServerAlias www.example.com
  DocumentRoot /www/siteroot
  # etc.
</VirtualHost>

# First subdomain you want to redirect
<VirtualHost 127.0.0.1:80>
  ServerName vhost1.example.com
  RewriteEngine On
  RewriteRule ^(.*)$ http://example.com/vhost1$1 [R]
</VirtualHost>

# Second subdomain you want to redirect
<VirtualHost 127.0.0.1:80>
  ServerName vhost2.example.com
  RewriteEngine On
  RewriteRule ^(.*)$ http://example.com/vhost2$1 [R]
</VirtualHost>
鹿! 2024-07-12 13:31:08

我为 Ubuntu 18.04 找到的解决方案与 这个 类似,但涉及 NetworkManager 配置:

  1. 编辑文件 /etc/NetworkManager/NetworkManager.conf,并将行 dns=dnsmasq 添加到 [main] 部分

    sudo 编辑 /etc/NetworkManager/NetworkManager.conf 
      

    应该看起来像这样:

    <前><代码>[主要]
    插件=ifupdown,密钥文件
    dns=dnsmasq
    ...

  2. 开始使用 NetworkManager 的 resolv.conf

    sudo rm /etc/resolv.conf 
      sudo ln -s /var/run/NetworkManager/resolv.conf /etc/resolv.conf 
      
  3. 使用通配符配置创建文件

    echo '地址=/.localhost/127.0.0.1' |   sudo tee /etc/NetworkManager/dnsmasq.d/localhost-wildcard.conf 
      
  4. 重新加载 NetworkManager 配置

    sudo systemctl reload NetworkManager 
      
  5. 测试它

    挖掘 localdomain.localhost 
      

您还可以添加任何其他域,这对于使用本地开发设置时的某些类型的身份验证非常有用。

echo 'address=/.local-dev.workdomain.com/127.0.0.1' | sudo tee /etc/NetworkManager/dnsmasq.d/workdomain-wildcard.conf

然后这有效:

dig petproject.local-dev.workdomain.com

;; ANSWER SECTION:
petproject.local-dev.workdomain.com. 0 IN   A   127.0.0.1

The solution I found for Ubuntu 18.04 is similar to this one but involves NetworkManager config:

  1. Edit the file /etc/NetworkManager/NetworkManager.conf, and add the line dns=dnsmasq to the [main] section

    sudo editor /etc/NetworkManager/NetworkManager.conf
    

    should look like this:

    [main]
    plugins=ifupdown,keyfile
    dns=dnsmasq
    ...
    
  2. Start using NetworkManager's resolv.conf

    sudo rm /etc/resolv.conf
    sudo ln -s /var/run/NetworkManager/resolv.conf /etc/resolv.conf
    
  3. Create a file with your wildcard configuration

    echo 'address=/.localhost/127.0.0.1' | sudo tee /etc/NetworkManager/dnsmasq.d/localhost-wildcard.conf
    
  4. Reload NetworkManager configuration

    sudo systemctl reload NetworkManager
    
  5. Test it

    dig localdomain.localhost
    

You can also add any other domain, quite useful for some types of authentication when using a local development setup.

echo 'address=/.local-dev.workdomain.com/127.0.0.1' | sudo tee /etc/NetworkManager/dnsmasq.d/workdomain-wildcard.conf

Then this works:

dig petproject.local-dev.workdomain.com

;; ANSWER SECTION:
petproject.local-dev.workdomain.com. 0 IN   A   127.0.0.1
原来是傀儡 2024-07-12 13:31:08

我必须对我的一个网站做同样的事情。 您可以按照以下步骤操作

  1. 如果您的服务器上有cPanel,请创建一个子域*,如果没有,您必须在DNS中设置A记录(用于BIND请参阅 http://ma.tt/2003/10/wildcard- dns 和子域/)。 在你的开发上。 如果您的服务器上有 cPanel ,您最好通过将每个子域添加到您的 hosts 文件中来伪造子域。

  2. (如果您使用 cPanel,则无需执行此操作)。 您必须将如下内容添加到您的 apache vhosts 文件中。 这很大程度上取决于您运行的服务器类型(共享或非共享)。 以下代码不完整。 这只是为了提供方向。 注意:ServerAlias example.com *.example.com 很重要。

       
              文档根目录 /var/www/   
              服务器名称 example.com   
              服务器别名 example.com *.example.com   
       
      
  3. 下一步,因为您可以使用 PHP 脚本检查“Host”标头并找出子域并相应地提供内容。

I had to do exactly the same for one of my sites. You can follow the following steps

  1. 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 your hosts file.

  2. (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.

    <VirtualHost 127.0.0.1:80>  
            DocumentRoot /var/www/  
            ServerName example.com  
            ServerAlias example.com *.example.com  
    </VirtualHost>
    
  3. Next, since you can use the PHP script to check the "Host" header and find out the subdomain and serve content accordingly.

如歌彻婉言 2024-07-12 13:31:08

首先,我想让用户
发布页面并给每个页面一个
他们选择的子域(例如:
user.mysite.com)。 尽我所能
聚集,最好的方法是
将 user.mysite.com 映射到 mysite.com/user
与 mod_rewrite 和 .htaccess - 是
正确吗?

使用虚拟主机可能会更好。 这样,每个用户都可以拥有一个几乎独立于其他用户的网络服务器配置。

语法是这样的:

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

First, I want to allow users to
publish pages and give them each a
subdomain of their choice (ex:
user.mysite.com). From what I can
gather, the best way to do this is to
map user.mysite.com to mysite.com/user
with mod_rewrite and .htaccess - is
that correct?

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:

<VirtualHost *:80>
    DocumentRoot /var/www/user
    ServerName user.mysite.com
    ...
</VirtualHost>
浅听莫相离 2024-07-12 13:31:08

从我在许多网络主机上看到的情况来看,他们在 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.

南七夏 2024-07-12 13:31:08

我使用的是 Ubuntu 16.04,从 14.04 开始,我使用 Dave Evans 提供的解决方案 在这里,它对我来说效果很好。

  1. 安装dnsmasq

    sudo apt-get install dnsmasq 
      
  2. /etc/dnsmasq.d 目录下创建新文件 localhost.conf以下行

    #file /etc/dnsmasq.d/localhost.conf 
      地址=/localhost/127.0.0.1 
      
  3. 编辑 /etc/ dhcp/dhclient.conf 并添加以下行

    前缀域名服务器 127.0.0.1; 
      

    (您可能会发现该行已经存在,您只需取消注释即可。)

  4. 最后一个是重新启动服务

    sudo systemctl restart dnsmasq 
      须藤 dhclient 
      

最后,您应该检查它是否正常工作。

dig whatever.localhost

注意:

如果您想在网络服务器上使用它,只需将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.

  1. Install dnsmasq

    sudo apt-get install dnsmasq
    
  2. Create new file localhost.conf under /etc/dnsmasq.d dir with the following line

    #file /etc/dnsmasq.d/localhost.conf
    address=/localhost/127.0.0.1
    
  3. Edit /etc/dhcp/dhclient.conf and add the following line

    prepend domain-name-servers 127.0.0.1;
    

    (You’ll probably find that this line is already there and you just need to uncomment it.)

  4. Last one is restart the service

    sudo systemctl restart dnsmasq
    sudo dhclient
    

Finally, you should check if it's working.

dig whatever.localhost

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.

巴黎夜雨 2024-07-12 13:31:08

我意识到我回答这个问题已经很晚了,但我在本地开发解决方案方面遇到了同样的问题。 在另一个SO线程中我发现更好的解决方案,并认为我将来会与任何有相同问题的人分享:

VMware 拥有的通配符域将任何子域解析为 127.0.0.1:

vcap.me resolves to 127.0.0.1
www.vcap.me resolves to 127.0.0.1

或为了获得更多功能 37 Signals 拥有一个域,可以使用以下命令将任何子域映射到任何给定的 IP:具体格式:

127.0.0.1.xip.io resolves to 127.0.0.1
www.127.0.0.1.xip.io resolves to 127.0.0.1
db.192.168.0.1.xip.io resolves to 192.168.0.1

请参阅 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:

vcap.me resolves to 127.0.0.1
www.vcap.me resolves 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:

127.0.0.1.xip.io resolves to 127.0.0.1
www.127.0.0.1.xip.io resolves to 127.0.0.1
db.192.168.0.1.xip.io resolves to 192.168.0.1

see xip.io for more info

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文