如何在同一个域中实现不同的 URL 开始? [英] How can I achieve different URL start in the same domain?

查看:63
本文介绍了如何在同一个域中实现不同的 URL 开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我开发了一个网站,假设它的域为 foo.bar.

Let's suppose I develop a website, let's suppose it has a domain of foo.bar.

让我们进一步假设我有一个名为 lorem 的用户和一个名为 ipsum 的用户.我想为 lorem 创建一个 lorem.foo.bar 的 URL,为 ipsum 创建一个 ipsum.foo.bar 的 URL.我可以使用哪些可能的技术来实现这一目标?你们能举出任何实现这一点的技术的例子吗?

Let's suppose further that I have a user called lorem and a user called ipsum. I would like to create an URL of lorem.foo.bar for lorem and a URL of ipsum.foo.bar for ipsum. What are the possible technologies with which I can achieve that? Can you guys give any example in any technology where this is achieved?

非常感谢.

推荐答案

根据您的需求,我发现这种方法是最直接的.

Depending on your needs I find this approach to be the most straightforward.

根据我的经验,最好的方法是通过在您的 DNS 服务器上配置的通配符域.在您的示例中 *.foo.bar 将是您的 foo.bar 域的通配符.通常这些是在 A 记录上设置的,但可以在 MX 记录等上设置.这里唯一真正的要求是您的 DNS 服务器支持通配符域.

In my experience the best way to do what you are trying to do is through Wildcard domains which is configured at your DNS server. In your example *.foo.bar would be the wildcard for your foo.bar domain. Typically these are setup on A records but can be setup on MX records and the like. The only real requirement here is that your DNS server supports wildcard domains.

就网络服务器而言,您将需要采用某种 URL 重写技术,以便 lorem.foo.bar 显示 lorem 页面等.有几种技术可以重写 URL.在 Apache 上,您可以配置 VirtualHosts 或使用 mod_rewrite.在 IIS 上,同样有几种使用 ISAPI 过滤器的方法,或者您可以使用 ASP.NET MVC 中的 URL 路由来进行重写.

As far as the Webserver goes, you will need to employ some sort of URL Rewriting technique so that lorem.foo.bar displays the lorem page and so on. There are several techniques to ReWrite URLs. On Apache you can configure VirtualHosts or use mod_rewrite. On IIS there are, again, several methods using either ISAPI filters or you can use URL Routes in ASP.NET MVC to do the rewriting.

以下是一些相关示例:

使用 Apache VirtualHost 创建通配符子域

.NET 4 中的 URL 重写?

http://support.microsoft.com/kb/840687

http://www.isapirewrite.com/docs/

-按评论编辑

在您的 *.foo.bar 示例中,您只会在您的 DNS 服务器上设置一个通配符域.一旦完成,除非您添加不同的通配符,否则您不必再次这样做.

In your example of *.foo.bar you will only ever setup a single wildcard domain on your DNS server. Once that is done you shouldn't ever have to do it again unless you add different wild cards.

就 Apache 而言,你应该能够做这样的事情(我很长时间没有使用 Apache,所以这可能有点不准确)

As far as Apache goes you should be able to do something like this (I haven't used Apache in a long time so this might be slightly inaccurate)

<VirtualHost 0.0.0.0>
   DocumentRoot /www/[directory sub domains are served out of]
   ServerName www.foo.bar
   ServerAlias *.foo.bar
</VirtualHost>

同样,这应该是一次性配置,或者当您添加更多通配符时.从这里开始,您的 PHP(或您使用的任何语言)将需要根据给定的子域处理它应该显示的内容的实际处理.

Again this should be a one time configuration or as you add more wildcards. From here your PHP (or whatever language you are using) would need to handle the actual processing of what content it should display based on the given subdomain.

--为第二条评论编辑 2

--edit 2 for second comment

所以通配符域几乎允许任何值.在上述情况下,*.foo.bar 将允许 lorem.foo.bar、ipsum.foo.bar、someguy.foo.bar 等,而无需您向 DNS 服务器添加其他条目或添加其他 VirtualHost 条目.因此,使用上面的 VirtualHost 配置可以说 DocumentRoot 是/www/subdomains.当导航到 lorem.foo.bar 或 ipsum.foo.bar 时,两个请求都从/www/subdomains 文件夹中获取,因此那里的代码需要获取子域的名称(在这种情况下为 lorem 或 ipsum),然后通过查询数据库或您拥有的任何其他业务流程来对这些值采取行动.尝试为此过程创建单个目录、DNS 条目和/或 VirtualHost 条目是错误的,因为这将成为绝对的管理噩梦.在上面的示例中,您只需要担心 1 个 DNS 条目、1 个 VirtualHost 条目和一个包含图像、网页、PHP 脚本等的目录.

So a wildcard domain allows for pretty much any value. In the above case *.foo.bar would allow for lorem.foo.bar, ipsum.foo.bar, someguy.foo.bar, and so on without requiring you to add additional entries to your DNS server or add additional VirtualHost entries. So using the VirtualHost configuration above lets say the DocumentRoot was /www/subdomains. When navigating to lorem.foo.bar or ipsum.foo.bar both requests get served out of the /www/subdomains folder so the code in there would need to grab the name of the subdomain (lorem or ipsum in this case) and then act on those values by querying the database or whatever other business processes you have in place. Attempting to create a single directory , DNS entry, and/or VirtualHost entry for this process is a mistake because it will become an absolute management nightmare. In the example above you only ever have to worry about 1 DNS entry, 1 VirtualHost entry, and one directory worth images, web pages, PHP scripts, etc.

这篇关于如何在同一个域中实现不同的 URL 开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆