Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to create a basic PHP router

Abel Lifaefi Mbula

When you begin your PHP journey, chances are that you will primarily hard code file paths in the URL to access them.

Let’s say you have a project called platform with the following files.

  • index.php: welcome note
  • courses.php: list of courses
  • authors.php: list of authors
  • aboutus.php: about the platform

How beginners navigate files

Imagine you want to access a list of all authors. What do you do? You type on the URL.

It works, but what if you decide to change the name of the file one day? You’ll have to fix it everywhere you called this link.

Another issue with this way of directly calling files is that you reveal the project’s internal structure to the world, which is unsafe for security purposes.

Lastly, the URL does not look elegant. For example, calling a URL like is more elegant than

Let’s see how we can do better.

How professionals navigate files

To do things better, we’ll need to create a routing system, which is a technique that consists of mapping all HTTP requests to a request handler. We will:

  • redirect all HTTP requests to index.php (the router)
  • create a routing system with switch

Redirect all HTTP requests to the router

To redirect all HTTP requests to the router, create a file named .htaccess on the root of the project and redirect all traffic to index.php. This is shown below:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php 

In our .htaccess file, we first activate the Apache server’s runtime rewriting engine. We limit access to physical files with Apache RewriteCond directive. Then, we redirect all upcoming requests to the index.php file.

.htaccess is a configuration file for the Apache server. It has no extension, but starts with a dot (.) for a hidden file.

In case your app/site is not at the root of your server, or if you don’t have a virtual host, you’ll need to create your .htaccess. This is shown below:

RewriteEngine On
RewriteBase /<folder>/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /folder/index.php [L]

Note: Replace <folder> with the name of the folder containing your site.

Create a routing system with switch

Our routing system will work as follows:

  • get the user requested path with $_SERVER super global variable
  • require the corresponding page


Add the code below after an index.php file is created.


$request = $_SERVER['REQUEST_URI'];

switch ($request) {

    case '':
    case '/':
        require __DIR__ . '/views/index.php';

    case '/courses':
        require __DIR__ . '/views/courses.php';

    case '/views/authors':
        require __DIR__ . '/views/authors.php';

    case '/about':
        require __DIR__ . '/views/aboutus.php';

        require __DIR__ . '/views/404.php';
The router, index.php

We have defined our four routes plus a default one if a user requests a path that does not exist. In this latter case, we would display a custom Not Found page.

Create the views

All our files live in the views directory.

Create it and place the files:

<p>Welcome to the platform.</p>
<p>List of our courses.</p>
<p>List of our authors.</p>
<p>We are leaders in courses publications.</p>
<p>You've reached the end of Internet.</p>



View all Courses

Keep Exploring