Search code examples
playframeworkplayframework-1.x

redirect to error page, if Url is invalid in play-framework. 1.25?


How can I redirect to error page, if user enters invalid urls. Right now, in case of invalid URL play shows me routes.

These routes have been tried, in this order :

   Not found
    GET /log

    These routes have been tried, in this order :


    GET       /@documentation/cheatsheet/{category}             PlayDocumentation.cheatSheet
        GET       /@documentation/modules/{module}/files/{name}     PlayDocumentation.file
        GET       /@documentation/modules/{module}/images/{name}    PlayDocumentation.image
        GET       /@documentation/modules/{module}/{id}             PlayDocumentation.page
        GET       /@documentation/files/{name}                      PlayDocumentation.file
        GET       /@documentation/images/{name}                     PlayDocumentation.image
        GET       /@documentation/{id}                              PlayDocumentation.page
        GET       /@documentation/?                                 PlayDocumentation.index
        GET       /public/                                          staticDir:public

I do want to show routes to user for many reasons. How can i redirect to error page?


Solution

  • You can modify the provided app/views/errors/404.html to provide a different 404 view.

    The route listing should only display in DEV mode for ease of debugging. In PROD mode, the user should just see 'Not Found'.

    Alternatively, you can implement afterActionInvocation() in a custom plugin (https://www.playframework.com/documentation/1.2.5/api/play/PlayPlugin.html) that checks for response status code 404 and then redirect to an entirely different route.