51

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.

4
  • I thought about it... I don't know. it's something that does accept a correct answer, but it's very broad. I'll set it to CW, though. We are two thinking the same Dec 27, 2009 at 9:52
  • @sar no. The patent reads on unknown subdomains, not on using them at all.
    – bmargulies
    May 23, 2011 at 1:45
  • @margulies, hrm I didn't pay enough attention to the text on this page... it was correctly formulated for the (quickly closed) question where I started from. Thanks!
    – sarnold
    May 23, 2011 at 1:46
  • foo.example.com, where foo isn't specifically listed in DNS (see Wildcard DNS) but is used in a query on the web application, is patented in the United States despite its relative obviousness, so being able to switch to the second form if you get a licensing demand from the patent owner when using the first form would be very well worth the time.
    – sarnold
    May 23, 2011 at 1:48

10 Answers 10

30

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.

9

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.

3
  • What about common functionality, common cookies, common javascript? Dec 27, 2009 at 9:54
  • btw, nice avatar... it made me laugh :D Dec 27, 2009 at 9:55
  • Thanks about the avatar, and I thought this was more of a design question than about implementation -- Couldn't you use sub-directories but set up domain aliases and the like so that you use blah.com/admin, but that moves to admin.blah.com, which is just an alias for blah.com/admin? Forgive me if I'm doing something wrong... I'm no network guy, and I've only implemented this very simply, and appreciated other sites that do it (like google's apps) Dec 27, 2009 at 10:04
7
  • In terms of code organization: The differences are nil, as you can map subdomains to any directory.
  • In terms of browser security: JavaScript access across subdomains is possible but has obstacles (see 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.)

1
  • 1
    This is a good point, I guess there's no real answer to this question, it seems really subjective. Dec 27, 2009 at 10:05
2
  • You can easily 'do' virtual servers on subdomains.
  • You can separate out subdomains to different cookies.

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.

2

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.

2

There have been excellent answers already, so you could check out these links anytime.

2

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.

2
  • What if you are using same codebase and multiple DB, based on sub-directory name access the DB. In this approach also sub-directories are good or sub-domain in terms of security ? May 16, 2018 at 13:08
  • DBs are on the backend, but if you surface DB information to a cookie then both directory apps can access it. May 16, 2018 at 21:04
1

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

  • On the downside, there's likely to 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.

2
  • What does routing have to do with MVC?
    – troelskn
    Dec 27, 2009 at 12:13
  • @troelskn Nothing per se, but a /<controller>/<action> URL setup is quite common. Dec 27, 2009 at 12:35
0

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.