Tania Rascia

Skip Navigation
The Simplest PHP Router

The Simplest PHP Router

 /  13 responses

I wanted to create the absolute most basic routing code in PHP, so here it is. We will direct ALL traffic to index.php and route to the new files from there.

Redirect all requests to index.php

In the root of your project, create a .htaccess file that will redirect all requests to index.php.

.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php [QSA,L]

Create a routing switch

Get the requested path with $_SERVER['REDIRECT_URL'], and require the page you want to display. I have '' and '/' for both url.com/ and url.com.

index.php

<?php

$request = $_SERVER['REDIRECT_URL'];

switch ($request) {
    case '/' :
        require __DIR__ . '/views/index.php';
        break;
    case '' :
        require __DIR__ . '/views/index.php';
        break;
    case '/about' :
        require __DIR__ . '/views/about.php';
        break;
    default: 
        require __DIR__ . '/views/404.php';
        break;
}

Create the views

Create a /views directory and place the files.

/views/index.php
<h1>Main</h1>
/views/about.php
<h1>About</h1>
/views/404.php
<h1>404</h1>

That’s it!

Note

Hi, my name is Tania Rascia. I've turned down every offer for advertisements or sponsored posts on this website. I write free resources that have helped thousands of people successfully transition into a web development career.

My committment is to no bullshit, no sponsored posts, no ads, and no paywalls. If you enjoy my content, please consider supporting what I do.

Support my work

Newsletter

New articles on web development every two weeks.

Write a response

Your email address will not be published.

All code will be displayed literally.

Discussion

  • Robert says:

    Hey,
    Great work!

    I'm wondering about going to /views.

    I've copied your example, and it works fine, until I request /views.

    Then it gives me /views/index.php file, rather than 404.php.

    I'm wondering how to truly redirect all traffic to index.php in the root folder.

    Cheers

  • Matthew Fedak says:

    So no dynamic routing?

  • Taurus says:

    First of all, a very nice and simple article.

    How do one get this to work with nginx?

    Any pointers regarding nginx rewrite rules would be highly appreciated.

    Also can you provide an example with query strings and how to forward them to the target page.

    Thank You!

  • Alec Richardson says:

    Nice!

  • Jack Wan says:

    The switch statement is a bit cumbersome to maintain, you need to remember to edit index.php on every new file or file rename, instead of just calling the files in the folder.. perhaps, streamline that process…

  • Sai Charan says:

    Simple and useful, thanks for sharing 🙂

  • Anonymous says:

    Thankyou!

  • Jens Bröcher says:

    Very helpful! No need for bulky frameworks with thousands of files.

  • Anonymous says:

    I lost an hour because of a stupid typo in your code 🙁

    `RewriteCond %{REQUEST_FILENAME}%` there's a "%" at the end that shouldn't be there ("%{REQUEST_FILENAME}" and not "%{REQUEST_FILENAME}%").

    • Àngel says:

      Wow, no need to be rude. It's a free tutorial on the internet , it happens sometimes (even with paid tutorials…).

      Tania! Big fan here, do you think you could make a tutorial about dynamic routing? I guess it's not super complex (I'm about to Google it right now) but you have a very clear and direct way of explaining things that I really like (I love your React tutorial, much better than almost anything else I've bumped into).

      Thanks and keep up the good work!