Search code examples
regexzend-frameworkurl-rewritingzend-route

Zend route regex for one character in url


For an web-application i need to handle the following url structure:

http://foo.tld/a/foo

I need to write a regex for my route to handle /a/ this part has to contain only 1 character as possibility.

I handle now with:

routes.suggestions.type = "Zend_Controller_Router_Route_Regex"
resources.router.routes.suggestions.route = ":character/:q"
resources.router.routes.suggestions.defaults.module = "default"
resources.router.routes.suggestions.defaults.controller = "index"
resources.router.routes.suggestions.defaults.action = "search"
resources.router.routes.suggestions.defaults.character = ""

But if conflict with other controller urls such as '/ajax/index' because I overwrite it now.

Can someone help me to place a regex on the route above to make ':character' just available for one single character?

Regards, Nick


Solution

  • Try adding this line:

    resources.router.routes.suggestions.reqs.character = "\w"
    

    That will add the regex \w (match 1 character a-z) to the :character parameter in the URL. If you add that, it will only route to SearchController if the URL parameter before the :q is 1 character long.

    See also Using Zend_Config with the RewriteRouter.