double slash in URLs.

拥有回忆 提交于 2019-12-14 03:11:49

问题


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+"

回答1:


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"


来源:https://stackoverflow.com/questions/11072856/double-slash-in-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!