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
4
  • Do you only want to redirect that structure?
    – Cyclone
    Nov 28, 2012 at 19:21
  • Dude wrong approach! You need something like this ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ yourscrpit.php?var1=$1&var2=$2 in your .htaccess
    – pregmatch
    Nov 28, 2012 at 19:22
  • 1
    @pregmatch But it has to work with classes and methods with unlimited parameters, i.e. classname/method/param1/param2/param3 etc. Nov 28, 2012 at 19:25
  • 4
    @pregmatch, dude, brittle approach :) Nov 28, 2012 at 19:26

3 Answers 3

10

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

FallbackResource /index.php
5
  • How would I implement this in my case? Haven't heard of this before. Nov 28, 2012 at 19:28
  • Reading the links I provided should be a good start. After that, check out PHP functions like parse_url. Nov 28, 2012 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, 2012 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. Nov 28, 2012 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 :). Nov 28, 2012 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.

2
  • It's wasteful to enable mod_rewrite and never rewrite the URL. Nov 28, 2012 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, 2012 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.