Routing URL Path with PHP and Apache

imperium2335 picture imperium2335 · Nov 28, 2012 · Viewed 15.2k times · Source

I am trying to create a nice url structure for my site.

My router class will only work if the url is in the style of ?something=value.

How do I get it so it will work like:

/something/value

In my .htaccess I have:

Options FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(js|txt|gif|jpg|png)$ index.php?$1 [L,QSA]

And in my router class I'm making:

class init {

    function __construct()
    {

    $URL = substr($_SERVER['REQUEST_URI'], 19) ;
    $URLElements = explode('/', $URL) ; // Adjust if needed.

    $class = $URLElements[0] ;
    $method = $URLElements[1] ;

    if(($t = substr_count($URL, '/')) > 1)
    {
        for($i=2;$i<$t+1;$i++) {
            echo $URLElements[$i].'<br />';
        }
    }
    }

}

Thanks to Jason, my .htaccess is now just:

FallbackResource /t2013/public_html/index.php

Answer

Jason McCreary picture Jason McCreary · Nov 28, 2012

For a quick way to handle Front-end Controllers with Apache, use FallbackResource and parse the URL with PHP.

FallbackResource /index.php