Frage

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
War es hilfreich?

Lösung

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

FallbackResource /index.php

Andere Tipps

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.

From a PHP perspective, you would just need to adjust your init class to key off of $_SERVER['REQUEST_URI'] instead of $_SERVER['QUERY_STRING']. You are not showing your full logic here, but my guess is that you would simply trim the first / and then explode the URI on / to get at the component parts.

From an .htaccess standpoint, you could just change your last line to be:

RewriteRule ^.*$ index.php [L,QSA]

You don't need to specifically detect images/css/js here as that RewriteCond already makes exceptions to the rewrite rule for any actual files or directories. Note that in the RewriteRule I did leave the Query String Append (QSA) flag in place in case you still wanted to do something like /controller?some=string&of=data If you don't anticipate using query strings at all with your new URI's, then you can omit this flag.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top