Beamtic's logo

Share via:

Subdomains vs Directories

Subdomains may be more flexible than directories when deciding for a location for a new web application, the main reason is that you can more easily setup extra servers as needed.

108 views

Edited: 2020-07-16 06:15

In this article I will argue that subdomains are more flexible than placing your different services in seperate directories; people who are very focused on SEO might not care about that, but even if you mostly focus on the SEO aspect, you should realize that how search engines perceives a particular setup could change over night—making decisions based on speculative short-term circumstances, of doubtful value, is just stupid.

Whether we should use a subdomain or a subdirectory is actually not very hard to decide. If you want to host an e-mail server, then it is technically more flexible to host it on a subdomain than it is to host it on your main domain name. As you may know, HTTP(S) runs on port 80 and 449, which means that we can still have e-mail services running on other ports; however, if we plan to use a caching proxy like Cloudflare, we will simply need to use a subdomain, otherwise we will not be able to access the e-mail services.

Of course, you typically do not allow HTTP(S) access to your e-mail servers; but if you did, that would also be reason enough to host them on a subdomain, quite simply because they would block your main site otherwise. It could be you had a setup guide for your users, if they attempted to type in the e-mail server-address in their browser.

The above is, perhaps, very obvious; but what about hosting different web-app services? You could have a Wordpress blog located in example.com/blog/, and a static website at example.com/; but, would that be a good idea?

Moving a site from a directory to a subdomain (or vice versa)

Before deciding, we should consider the broader requirements of the website or business; and let us also remember that we can, most probably, always change our mind later. All it takes is a small application migration by someone with enough back-end experience to carry it out safely.

We should not be afraid to loose SEO value by moving something that has been properly redirected. It takes some time for search engines to catch up with the new URLs, but doing this time, the old URLs should still be indexed. If configured properly, Google is, in my experience, very effective at picking up redirects.

Choosing a subdomain for different web apps

Placing a web- application in a directory can make it very inflexible, and it will influence all other applications that you run on that domain. Both today, and in the future! I am not saying you should not do it—but you should be aware of the negative effect it might have on your sites potential to evolve.

Technically, all web applications you place in directories will steal valuable URLs that are then not available to other applications. If you place something on /blog/, suddenly you might not be able to create an article from your root "/" application with that URL. This is rarely going to be a problem, and even if it does happen, you could just name the URL something else to get around it.

However, what then if you wanted to host the blog application on a separate server? Well, suddenly you would have to spend time migrating the application to a subdomain, which might, if done improperly, result in broken URLs and confused users. If you had chosen a subdomain from the start, you could just create a DNS record and point the subdomain to the extra server.

Another way to get around "rare" URL conflicts, is to place everything under a URL segment (/some-segment/; the segment may or may not map to a physical directory on the file system—URLs can be "rewritten" to point to different physical locations in the file system—but, the point is, placing things under a segment is not very user-friendly. If for example I was to place all of my articles under /article/name-of-article, then the URL would be longer and harder to read; it also adds redundant information to the user, which is usually self-evident when they look at the URL, title, and description.

Security and directories

Subdomains has traditionally been used to keep things separated. The www subdomain was used to differentiate between the web service and other services the users relied on, such as POP or SMTP.

Another use for subdomains is for security purposes, since by using them to host web applications, we may avoid compromising our entire server if one application is either hacked or breaks.

Wordpress sites has been notorious for getting hacked due to security issues with various user-created Plugins, and if a Wordpress site gets compromised while having write-access to the file system, then you could potentially be looking at a complete re-install of other apps as well. Hosting the site on a separate server would effectively contain potential incidents to the specific server Wordpress is running on.

With cloud hosting and the ability to launch new servers with the click of a button, such a setup will add very little extra maintenance.

Comments

  1. HTTP headers are not case-sensitive, so we are free to convert them to all-lowercase in our applications.
  2. In-app browsers do some things a little differently than normal browsers, and that might result in 500 errors on your server, among other interesting things.
  3. Short explanation of the HTTP range header.
  4. Learn why 403 is the most suitable status code for failed log in attempts.
  5. How to block unused URL parameters (non-existent) in PHP web applications.

More in: Web development