Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing URL Path with PHP and Apache

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
like image 604
imperium2335 Avatar asked Oct 16 '25 10:10

imperium2335


2 Answers

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

FallbackResource /index.php
like image 144
Jason McCreary Avatar answered Oct 19 '25 01:10

Jason McCreary


htaccess should be something like this

RewriteEngine on
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)$ $1.php?$2=$3 [QSA]

so for example

/home/something/value would be redirected to /home.php?something=value

Give it a go, not completely sure on this but have done something similar before.

like image 33
Grant Carlisle Avatar answered Oct 18 '25 23:10

Grant Carlisle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!