Web 编程中的子域与子目录
处理网络上的多个“应用程序”有两种主要策略:
- 子域名(例如 wiki.example.org、blog.example.org、admin.example.org、api.example.org/v1)
- 子目录(例如 example.org) /wiki, example.org/blog, example.org/admin, example.org/api/v1)
这两种解决方案在处理 Web 编程时(例如在代码组织、浏览器方面)有什么区别(优点和缺点)安全模型、javascript 等)。
编辑:CW,因为有一个正确的答案,但它非常广泛。
There are two main strategies for handling multiple "applications" on the web:
- subdomains (e.g. wiki.example.org, blog.example.org, admin.example.org, api.example.org/v1)
- subdirs (e.g. example.org/wiki, example.org/blog, example.org/admin, example.org/api/v1)
What are the differences (advantages and disadvantages) of these two solution when dealing with web programming (e.g. in terms of code organization, browsers security models, javascript etc).
Edit: CW as there's a correct answer, but it's very broad.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
除了从安全角度来看在子域中隔离应用程序更容易这一事实之外,我将仅评论我认为两者之间最大的区别。
子域的专业版:
缺点:
我的建议:
确保您始终可以两者兼得,这将为您带来两全其美的效果。应用程序的每个部分都应该有一个始终受到尊重的可配置基本 uri。只要你确保你总是可以双向前进,那么谁在乎你做什么呢?它只是一个 url,并且可以随时更改。
Besides the fact that from a security standpoint it is a bit easier to isolate an app within a subdomain, I will just comment on what I think is the biggest difference between the two.
Pro's for subdomains:
Cons:
My advice:
Make sure you can always do both, which will give you the best of both worlds. Every part of your app should have a configurable base uri that is always respected. As long as you make sure you can always go both ways, then who cares what you do? it's just a url and it can always be changed.
就我个人而言,我更喜欢为每个应用程序使用子域,然后是子目录(无论它们是否实际上是子目录 - 最好它们只是通过.htaccess)来表示该应用程序的不同状态。例如:
admin.blah.com/users/1234/bob
,admin.blah.com/pages/4321/title
,blog.blah.com/archives/2007/5678/title
,子域告诉您
您在哪里,子目录告诉您您在做什么。
Personally, I prefer using a subdomain for each application, and then the sub-dirs (whether they are actually sub-directories or not -- preferably they are just re-routed to
/index.php
by a .htaccess) to denote different states of that application. For example:admin.blah.com/users/1234/bob
,admin.blah.com/pages/4321/title
,blog.blah.com/archives/2007/5678/title
,etc.
The subdomain tells you where you are, and the sub-directories tell you what you're doing.
document.domain
和配偶)。我不知道 JavaScript 方面有什么在处理不同子域时完全不可能的事情。
意见:
我个人倾向于目录,反对公共地址的子域。公众已经习惯了以“www”开头的网址。打破这种模式会造成不必要的混乱。您会注意到,很多时候,当人们在地址栏中输入子域名时,他们会自动开始输入“www”。他们会惊讶地发现地址可以没有。
对我来说,使用子域的唯一好方法是出于内部目的,以方便或准备使用不同的服务器(例如 static.example.com、images.example.com 等)
document.domain
and consorts). I do not know of anything on the JavaScript side that is completely impossible when working with different sub-domains.Opinion:
I personally tend towards directories and against subdomains for public addresses. The general public have become used to web addresses beginning with "www." and it creates unnecessary confusion to break this pattern. You will notice that very often people, when given a subdomain to type into the address bar, will automatically start typing in "www." and they will be surprised to learn that an address can be without.
To me, the only good way to use subdomains is for internal purposes to facilitate, or prepare for, the use of different servers (e.g. static.example.com, images.example.com etc.)
已经有很好的答案,因此您可以随时查看这些链接。
子域和子目录
子域与子目录
There have been excellent answers already, so you could check out these links anytime.
Subdomains and subdirectories
Sub Domain Vs Sub Directory
我认为使用子域(而不是子/目录)的三个最重要的原因是安全、安全和安全。子/目录将服务器会话暴露给黑客。
例如,如果我将两个网络应用程序放入目录中,那么它们通常会共享同一个会话(除非采取特定步骤来防止/维护它)。因此,如果我有:
domain.com/application1
domain.com/application2
如果 application2 被黑客攻击,那么黑客可以看到 application1 中设置的所有会话变量。此时,您的会话安全性已降低到最弱子应用程序的级别,即链中最薄弱的环节。
I think the three most important reasons for using sub.domains, as opposed to sub/directories, are security, security and security. Sub/directories expose the server session to hackers.
For example, if I have two web apps that I put into directories then they will typically share the same session (unless specific steps are taken to prevent/maintain it). So if I have:
domain.com/application1
domain.com/application2
If application2 gets hacked then the hacker can see all the session variables that were set in application1. At this point your session security is reduced to the level of the weakest subapp, i.e., the weakest link in the chain.
您最好认识到子域是网络空间中的“主要”分隔,而子目录是“次要”分隔。子域名适用于不同的域名;您可以让不同的人在不同的子域上运行不同的应用程序。子目录是单个(子)域的分区,可能由同一用户分隔不同的应用程序。
Web 标准故意非常开放,但你越滥用它们来创建奇怪的层次结构,最终就会越痛苦。
You'd be best off recognizing that subdomains are a "major" separation in Web space, and subdirectories a "minor" one. Subdomains are for, well, different domains; you could have different people running different applications on different subdomains. Subdirectories are partitions of a single (sub)domain, separating perhaps different applications by the same user.
Web standards are intentionally very open, but the more you abuse them to create strange hierarchies, the more that will bite you in the end.
子域的主要优点之一是它们指向的文件可以包含在任何地方,甚至可以包含在另一台服务器上。我对子域最常见的用法是正在进行的实时项目的开发。例如,您可以创建子域:
dev.example.com
并复制您的实时站点,包括整个目录结构。放入 .htaccess 文件以拒绝除您和您客户端的 IP 之外的任何人的连接,并使用该文件进行更改,直到准备好实时推送更新为止。
One of the major advantages of sub-domains is the files they point to can be contained anywhere -- even on another server. My most common usage of a sub-domain is on going development of a live project. For example, you could create the sub-domain:
dev.example.com
and make a copy of your live site, including the entire directory structure. Drop in an .htaccess file to refuse connections from anyone but you and your client's IP, and use this to make changes until the updates are ready to be pushed live.
这是一个相当大的主题,但这里有一些关于子域的想法...
优点
可以使用“干净”的路由设置,如果
您正在使用 MVC。
各个部分之间的划分非常清晰
系统和命名空间没有危险
重叠。
缺点
上的重复代码更多
后端,除非你很聪明
库/包含目录和使用
一个共享区域。
从SEO的角度来看,没有
这些天有很大的不同
确实还有像 Google 这样的工具
可以指示 Analytics 使用特定的日志记录域。
This is a pretty big topic, but here’s some thoughts on sub-domains...
Pros
Can use a ‘clean’ routing setup if
you’re using MVC.
Very clear division between parts of
the system and no danger of namespace
overlap.
Cons
be more duplication of code on the
backend, unless you're clever with
library/includes directories and use
a shared area.
From an SEO perspective, there's not
a great deal of difference these days
and indeed tools such as Google
Analytics can be instructed to use a specific logging domain.
我个人使用flask进行开发
所以设置一个(两者)并不麻烦,
通常我会使用子目录
最好使用子域。
但在某些情况下,如果您有像
host.tld/user/repository/commit/... 这样的子目录,
我宁愿像
user.host.tld/repo/commit
那样,如果他们以另一种方式输入域,我通常使用会话进行重定向,
例如:如果他们像
host 一样输入域,我将重定向.tld/<用户>/<存储库>/<提交>
到
user.host.tld/repo/commit
让用户更容易,同时您可以以更好的方式安排您的应用程序
,简而言之,它确实是主观的
I personally use flask for development
so it isnt much of a hassle to set one up(both)
Usually i go with sub directories
but in some situations it is better to use subdomains
If you have subdirectories like
host.tld/user/repository/commit/...
Id rather go like
user.host.tld/repo/commit
And i usually use sessions to redirect if they type the domain in another way
eg:I will redirect the if they typed it like
host.tld/<user>/<repo>/<commit>
to
user.host.tld/repo/commit
Making it easier for the user meanwhile you can arrange your application in a better way
in short its really subjective
这取决于您的喜好。出于SEO目的,子目录可以更好地提高域名权重。
就个人而言,我对相关的不同应用程序使用子目录,同时对托管在不同服务器上的静态资产使用子域,以减少对子目录的请求数量。
It depends on your preference. For SEO purpose, subdirectory is better to boost the domain authority.
Personally, I used subdirectories for different apps that are related while I used subdomain for static assets hosted on a different server to reduce the number of requests to the subdirectories.