4

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
imperium2335
  • 23,402
  • 38
  • 111
  • 190

3 Answers3

10

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

FallbackResource /index.php
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • How would I implement this in my case? Haven't heard of this before. – imperium2335 Nov 28 '12 at 19:28
  • Reading the links I provided should be a good start. After that, check out PHP functions like [`parse_url`](http://php.net/parse_url). – Jason McCreary Nov 28 '12 at 19:30
  • Interesting... hadn't run across that before in the many various front controller configurations I have worked with or seen posted about in places like StackOverflow. Very straightforward implementation. – Mike Brant Nov 28 '12 at 19:30
  • It was added as of Apache 2.2.17 (or cerca). I found it to be more performant in such cases. Agreed it is unused. May do a blog post soon. – Jason McCreary Nov 28 '12 at 19:33
  • @Jason McCreary That's pretty awesum, makes the whole process much simpler, I have edited my post to reflect what I now have in case you wish to comment further :). – imperium2335 Nov 28 '12 at 19:55
1

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.

0

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.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • It's wasteful to enable `mod_rewrite` and never *rewrite* the URL. – Jason McCreary Nov 28 '12 at 19:32
  • @JasonMcCreary I agree it is a performance drain (especially when using mod_rewrite in .htaccess). I honestly like your answer on this question better than mine, but was unaware of it. I will leave answer here in case poster opts not to use your solution or doesn't have ability to enable `mod_dir` for whatever reason. – Mike Brant Nov 28 '12 at 19:34