This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

chris.hotz.behofsits3130
chris.hotz.behofsits3130

I have to change my routing a little bit and so some links will not work anymore, but I plan to give the user some advices if he hits an 404. So is it possible to access the original route the user wants to visit within a 404-page?

xchattercho3568
xchattercho3568

Hi there,

With Laravel you can do something like this:

App::missing(function($exception)
{
   $triedToAccess =  Request::url(); // Here you get the url which the user tried to access
    // some operations....
    return Response::view('folder.view', array(), 404); // if you want you can pass the url info to the view
});

*The view should be created. I also haven't tested this, but it should work. You can also consider to create redirect rules so the old urls are redirected to the new ones, which might be the better approach. :)

Last updated

chris.hotz.behofsits3130
chris.hotz.behofsits3130

@xchattercho3568 thanks for your reply! I found another way, I added the following script to the Code section of my 404-page. It tests if the referrer (the page which was not found) is a blog slug and redirects to it :)

function onStart()
{
    $slug_raw = Request::segment(1);
    $slug = str_slug($slug_raw);

    if (!empty($slug)){
        $num = Db::table('rainlab_blog_posts')->where('slug', $slug)->count();

        if($num > 0){
            return Redirect::to('post/'.$slug);
        }
    }
}
jan-vince
jan-vince

I used same approach as @xchattercho3568 suggests in /404 page markup, like:

title = 404
url = "/404"
is_hidden = 0
==
<?php
function onInit(){
    $originalRequest = parse_url(Request::url());
    // $path = $originalRequest['path']; // return eg. /about.html
}
?>
==
...

1-4 of 4

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.