Search code examples
zend-frameworkzend-routezend-aclzend-router

double slash in URLs.


Zend Route issue.

Normally it works fine.

http://www.example.com/course-details/1/Physics-Newtons-Law

But if I type in an extra slash in the url, the noauthAction of my Error controller gets called.

Example of URL's that are not working.

http://www.example.com/course-details//1/Physics-Newtons-Law
http://www.example.com/course-details/1//Physics-Newtons-Law

Is there something I need to set in the route definition to allow extra slashes?

Routing in application.ini

resources.router.routes.viewcourse.route = "/course-details/:course_id/:title"
resources.router.routes.viewcourse.defaults.controller = course
resources.router.routes.viewcourse.defaults.action = view
resources.router.routes.viewcourse.defaults.title = 
resources.router.routes.viewcourse.reqs.course_id = "\d+"

Solution

  • You could use a controller plugin to fix common URL typos.

    /**
     * Fix common typos in URLs before the request
     * is evaluated against the defined routes.
     */
    class YourNamespace_Controller_Plugin_UrlTypoFixer 
        extends Zend_Controller_Plugin_Abstract
    {
        public function routeStartup($request)
        {
            // Correct consecutive slashes in the URL.
            $uri = $request->getRequestUri();
            $correctedUri = preg_replace('/\/{2,}/', '/', $uri);
            if ($uri != $correctedUri) {
                $request->setRequestUri($correctedUri);
            }
        }
    }
    

    And then register the plugin in your ini file.

    resources.frontController.plugins.UrlTypoFixer = "YourNamespace_Controller_Plugin_UrlTypoFixer"