Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Routes don't need to match exactly?
  • Something interesting I noticed while developing a site using FuelPHP. This is my routes.php file:
    <?php
    return [
     '_root_'  => 'calculator/index',  // The default route
     '_404_'   => 'static/404',    // The main 404 route
     
     // Static pages
     'about'   => 'static/about',
     'links'   => 'static/links',
    ];
    
    Going to [url=http://mysite/about]http://mysite/about[/url] works, but so does [url=http://mysite/about]http://mysite/about[/url].stuff. I tried this rule and noticed the same thing:
    '^about$'   => 'static/about',
    

    Is this intentional? At the moment, I'm rewriting an existing site using FuelPHP. The current site uses /about.php as the URL to the about page, but the new FuelPHP site will use /about. I want to do a 301 redirect from /about.php to /about so that it can be updated in search engines. I thought about doing this in my 404 controller method, but for some reason, with these routing rules going to /about.php brings up the about page (as does /about.anythingelse). /about.anythingelse should be returning a 404, not showing the about page - I don't want multiple URLs for the same content.
  • If you use a dot in the URL, everything after the dot will be considered the URL extension (like .html) and will be stripped by FuelPHP. So "http://mysite/about.stuff"; is actually "http://mysite/about"; with the extension ".stuff". FuelPHP is a URI segment oriented framework, URI extensions are completely optional, and don't take part in routing descisions. Only the Controller_Rest uses the extension to determine the desired result type (json,html,xml, etc).
  • Is there an inbuilt way to disable that behaviour, or do I need to do something similar to what the REST controller does - override the router method in my base controller and throw a 404 (or do a 301 redirect) if there's an extension?
  • No. extensions are not relevant in a URI segment routed framework, making routing decisions based on the extension is not a good design. I can't think of any implementation where it would be logical to throw a 404 based on an extension. But if you insist you can make an inline route, and define a closure that checks Input::extension(), and make your decisions based on that. You can make HMVC requests from there and return the result if you need to call a controller method.
  • SEO is one reason - If you have the same content at different URLs, it can hurt SEO. On a site redesign where you're making your URLs a bit nicer (like moving /about.php to just /about), the original URL should do a 301 redirect to the new one, it shouldn't return the page (otherwise you have both /about.php and /about displaying the page). That and I only want pages to be available at the URLs I choose, I don't like the ability to just throw random extensions on the URL and have the page still work :P
  • I don't really know what I'm talking about here, but I would suggest having all your controllers extending a common app controller (thinking like CakePHP, dunno if Fuel does the same or not, I'm very new) and then using the before() method on the base controller look at Input::extension(). Not sure how easy it is to get the current page using FuelPHP though (Input::uri() looks like a good candidate form the docs, let's assume it drops the extension too). It'd be nice if you could do something like:
    class BaseController extends Controller {
        function before() {
            if (strlen(Input::extension())) {
                Response::redirect(Input::uri(), 'location', 301); // maybe this works?
            }
        }
    }
    
  • Daniel 15, Deal with that in your rewrite rules, it's a lot quicker and less complex then doing it in FuelPHP. As I said, FuelPHP is like many other frameworks URI segment driven, the extension is not used, irrelevant and discarded. If you insist, make a class in app/classes, give it an _init() static method that checks Input::extension(), and redirects to the current URI + your enforced extension with a 301 if the extension is wrong. Then define this class in your app/config/config.php as an autoload class, so this _init() runs for every request.
  • Yeah that's also a good idea :)
  • Thanks for the feedback :)
    alexrussell wrote on Tuesday 8th of May 2012:
    I don't really know what I'm talking about here, but I would suggest having all your controllers extending a common app controller (thinking like CakePHP, dunno if Fuel does the same or not, I'm very new) and then using the before() method on the base controller look at Input::extension(). Not sure how easy it is to get the current page using FuelPHP though (Input::uri() looks like a good candidate form the docs, let's assume it drops the extension too). It'd be nice if you could do something like:
    class BaseController extends Controller {
        function before() {
            if (strlen(Input::extension())) {
                Response::redirect(Input::uri(), 'location', 301); // maybe this works?
            }
        }
    }
    
    I ended up doing something very similar:
     public function before()
     {
      // Redirect from old URLs (.php) and any others with an extension - These pages don't need one
      if (Input::extension() !== null)
      {
       Response::redirect(Input::uri(), 'location', 301);
      }
      
      parent::before();
     }
    
  • No probs, glad you got it sorted.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion