URL Routing: why your web application needs it.

Gabriel Guerra
3 min readSep 1, 2021

You want to write a simple web application from scratch. One of the things you’re going to really need is an URL Router. Why? Let's start from the begining.

What is an URL Router?

An URL Router is the component responsible for receiving requests and dispatching to whatever handlers you have. Let’s take a few steps back.

Front Controller — a true forgotten hero

In the past web application commonly used to use the Front Controller pattern which in this case, would be a handler evaluating URL parameters (requests). For instance, if receiving A would load Page A while when receiving B would load Page B and so on.

This approach would allow the developers to leverage template engines but it has some cons like the URLs being absolutely ugly as http://foo.com?p=your_param

Enters URL Rewrite

URL rewriting is a feature implemented by web servers like Apache and Nginx which allow us to transform an URL. So let’s take the first example:

http://foo.com?p=your_param

We could have, for instance http://foo.com.br/p/your_param

Why do I should care about beautiful URLs?

The short answer would be — because it’s good for SEO (Search Engine Optimization). Though a better SEO is desirable, a more complex answer is necessary here. There’s a couple of reasons we would like to have URL rewrite enabled, to name a few:

SECURITY — We can hide the files structure in the server. Otherwise an attacker could leverage this knowledge and try to exploit your application.

NAVIGATION — Cleaner and well structured URLs deliver a more concise and intuitive navigation. It’s related to content architecture, hierarchy and SEO as well. For example, when building a news website, you could have a category aggregator in you url or even use namespaced urls. Django has a good definition of why it's a good idea to have namespaced urls.

SIMPLICITY — Remember the Front Controller pattern? It provides a simpler and more efficient way of dealing with page requests. When combined with URL rewrite we have complete flexibility over what content is being delivered on what URL structure. For instance, we can have a template with placeholders being filled with variable content. One could argue this can be achieved with a dedicated template engine and it’s true.

The beauty here is that one solution don’t need to exclude others and it’s also true that a template engine is not always required.

When combined, URL Routers and Template Engines they're a powerful design solution to serve web content, separating layers and abstracting logic easily.

Now we can go back to the object of the article: URL Routers.

Remember — an URL Router is a component accepting requests and remapping those to desired content in a way your application doesn’t have to mirror your files structure.

It would be expected such a component to be present in several web frameworks for Ruby, PHP, Python, Java and even JavaScript. And in fact that's how it is, but in a way some people tend to ignore what's happening behind the framework magic. Laravel, Django, Flask, Rails, SpringBoot, ReactJS — all of them do url routing in some way.

The concept of having a component reponsible for handling requests is part of the modern web development architecture and it enables other important aspects like middlewares though that term is way older.

URL Routing in Web Frameworks

Laravel / PHP

use Illuminate\Support\Facades\Route;

Route::get('/greeting', function () {
return 'Hello World';
});

Django / Python

from django.urls import path

from . import views

urlpatterns = [
path('articles/2003/', views.special_case_2003),
path('articles/<int:year>/', views.year_archive),
path('articles/<int:year>/<int:month>/', views.month_archive),
path('articles/<int:year>/<int:month>/<slug:slug>/', views.article_detail),
]

Flask / Python

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"

Spring Boot / Java

package com.example.routingandfilteringbook;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class RoutingAndFilteringBookApplication {

@RequestMapping(value = "/available")
public String available() {
return "Spring in Action";
}

public static void main(String[] args) {
SpringApplication.run(RoutingAndFilteringBookApplication.class, args);
}
}

Those are examples were extracted from official documentation. As you can see, every framework has it's own way of implementing URL Routing but at the end of the day it's the same concept applied.

Thank you for reading and Happy Coding :)

--

--