Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Getting URLs Under a Page to Return 404 File Not Found


Reply

3 Posts   1297 Views

Avatar
Ben Gribaudo

31 March 2009 at 9:10am (Last edited: 31 March 2009 9:10am), Community Member, 181 Posts

Hello,

With a fresh install of SilverStripe, the "About Us" page is located at: http://localhost/about-us/
Going to any of the following URLs results in the "About Us" page being displayed instead of a 404 file not found error:
http://localhost/about-us/abc/
http://localhost/about-us/abc/abc/
http://localhost/about-us/abc/abc/abc/

The reason this happens is that the rounting process knows that the page is displayed by a controller and thinks that the "/abc" parts of the URL are route parameters matching to "$Action/$ID/$OtherID" in the routing rules.

This functionality makes sense if we are writing a complex controller, but when we're just serving up normal site pages, a 404 response seems appropriate to return for the "/abc" URLs. It seems like this would be the desired default behavior for Page_Controller (i.e. made a part of core).

What do y'all think?

One way to implement this--in Page_Controller:

   function handleRequest($request) {
      $action = $request->latestParam('Action');
      // if the Action param is set but the controller doesn't have a method to handle that action, display a 404 error
      if ($action && !$this->hasMethod($action)) {
         // dispaly the 404 file not found error message
      }
      
      return parent::handleRequest($request);
   }

Btw, what is the best way to display a 404 error page from the method above?

Thank you,
Ben

Avatar
ajshort

31 March 2009 at 9:29am Community Member, 244 Posts

I fixed this as part of the nestedurls branch already, which will most probably be merged back for 2.4 - see http://open.silverstripe.org/changeset/64140

Avatar
Ben Gribaudo

31 March 2009 at 10:20am Community Member, 181 Posts

Thanks for that tip, Andrew. I am really looking forward to when nestedurls becomes a part of core. Here, we're already building a couple of sites that use it, with plans to go live with them even though nestedurls is still in beta.