Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

subdomain vs. subdirectory in web programming

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.

question from:https://stackoverflow.com/questions/1965609/subdomain-vs-subdirectory-in-web-programming

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  • You can isolate configuration (for for example apache) per-domain.
  • It will be easier to migrate parts of your application to other machines. Sub-directories won't really give you this flexibility.
  • Instead of having to use a $baseUri variable in every html template, you can just assume the root of the app is always /.

Cons:

  • It will be much more annoying to quickly setup staging or temporary development environments. For every 'app' you will now need DNS of hosts-file entries and webserver configuration. With subdirectories you could drop the app in a directory, and go!
  • If you do ever have the requirement to deploy your application on a different system where using / is because of some odd policy not possible, some rewriting might be in order.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...