apache - URL Rewriting In PHP MVC Framework

I have made my own custom php framework which implements MVC design pattern. In it, all URLs are constructed like this

mysite.com/index.php?route=controller/function/parameters/go/here
mysite.com/index.php?route=products/shirts/99

etc

I have put in place htaccess to remove index.php?route= part from URLs to make them more SEF making them appear like this:

mysite.com/controller/function/parameters/go/here
mysite.com/products/shirts/99

I just want to append a suffix at the end of each URL like so:

mysite.com/controller/function/parameters/go/here.html
mysite.com/products/shirts/99.html

Probably htaccess can be put to use for acheiving that or possibly some other solutions.

Any ideas please?

Thanks in advance.

4 Answers

  1. Lorin- Reply

    2019-11-16

    I assume you're taking the 'nice' urls then rewriting them to your index.php with the request rewritten to a get var or 'route'.

    There are some problems you'll run into in the future, one will be how to deal with get vars in the request (if I recall dealing with a similar concept years ago).

    Personally, I would just setup your .htaccess to send all requests that are not valid files/directories to your bootstrap/index.php file. Then do the parsing in PHP, not in a overly complex mod_rewrite. You're already parsing the 'route' get var, why not just parse the whole request?

    At that point the first step of you index.php file would be to:

    • Remove the . extension (or identify the request as html/xml/json/etc)
    • Remove any kind of base URL.
    • Parse the remainder like you do the route get var.
  2. Luke- Reply

    2019-11-16

    If I understand you correctly, you say you have used htaccess to remove route= and you want to also remove a .html suffix. I think you are approaching it the wrong way.

    I would have all the links in your app pointing to your preferred URLs and then have htaccess PUT IN in the route= and the index.php elements

    So a link looks like this:

    <a href="mysite.com/controller/function/parameters/go/here.html" />
    

    and you can use an htaccess rule similar to Nicky's ton convert that into:

    mysite.com/index.php?route=controller/function/parameters/go/here.html
    

    In fact Nicky's will work fine. You have the option of removing the .html suffix in your index.php code or from the htaccess rule - easier from PHP in my opinion.

    If you want to remove .html using htaccess then this will probably work (untested):

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)\.html$ index.php?route=$1 [QSA,L]
    </IfModule>
    
  3. Marcus- Reply

    2019-11-16

    Like others, I think the appending needs to happen in your framework.

    I imagine that you have a function like: $html->link('products/shirts/99');

    That function should add the '.html'.

    Your url handler should look like:

    //$query products/shirts/99.html

    $l = explode ($query, '/'); $last = length($l); $l[$last-1] = substring($l[$last-1] , find($l[$last-1] ,'.html')) //I am doing the args from memory so please check them.

    This is how CakePHP's index.php would be modified. I suggest you look at that code as a guide.

  4. Marcy- Reply

    2019-11-16

    I'm not an expert but try this:

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php?route=$1 [QSA,L]
    </IfModule>
    

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>