Welcome to Abdul Malik Ikhsan's Blog

Zend Framework 2 : Handling Trailing Slash “issue”

Posted in Teknologi, Tutorial PHP, Zend Framework 2 by samsonasik on July 24, 2012

In fact, in web application, it’s better not to have two urls respond to the same thing. But we are a lazy programmer, right, RIGHT ? We don’t have a much time to remember the route where is end with “/” or not.
For example we have 2 url like this :
http://zf-tutorial/album/add – 200 – works fine
http://zf-tutorial/album/add/ – 404 – The requested URL could not be matched by routing.

To handle that, just add the empty “/” in the end of route segment :

    'route'    => '/[:controller[/[:action[/]]]]',

Or for “:slug” segment :

    'route'    =>  '/[:controller[/[:action[/[:slug[/]]]]]]',

I do not know if this is a best solution or not, but this is probably one of the easiest.

References :
1. http://zend-framework-community.634137.n4.nabble.com/Trailing-slash-td4655266.html

Tagged with:

16 Responses

Subscribe to comments with RSS.

  1. dzo said, on December 4, 2012 at 9:22 am

    This is pretty bad😦
    It’s much much better to handle trailing slashes in the server’s rewrite rules.
    Or at least have a separate route that matches when everything else failed and if it spots a trailing slash, it does a permanent redirect to a non-slash version of the url.

    • samsonasik said, on December 4, 2012 at 11:47 am

      instead of saying “pretty bad”, maybe invest your time to give real other solution. post a real ‘code’ to do that, and i will update my blog post.

  2. rokm92 said, on February 27, 2013 at 7:40 am

    You can add the following code to your .htaccess file in public folder, right after the RewriteEngine On:

    # The following removes trailing slash from URL
    RewriteRule ^(.+)/$ /$1 [R=301,L]

  3. blogbudie said, on March 18, 2013 at 6:00 pm

    mantep Tutorial nya mas… lanjut terus

  4. […] Ссылка на оригинал […]

  5. A PHP Programmer said, on August 28, 2013 at 12:01 am

    I have tried in place of controller route but it didnt work. Can you please help me on this? Please see the piece of code shared below.

    ‘options’ => array(
    ‘route’ => ‘/news’,
    ‘defaults’ => array(
    ‘controller’ => ‘News\Controller\Index’,
    ‘action’ => ‘index’,

    Also, it would be great if you can share any of the simple sample project you have developed in zend framework 2.

    • Bobs said, on September 7, 2013 at 5:05 am

      your route is all wrong. try:

      ‘options’ => array(
      ‘route’ => ‘/news[/:controller][/:action]’,
      ‘constraints’ => array(
      ‘controller’ => ‘[a-zA-Z][a-zA-Z0-9-]*’,
      ‘action’ => ‘[a-zA-Z0-9-]+’,
      ‘defaults’ => array(
      ‘__NAMESPACE__’ => ‘News\Controller’,
      ‘controller’ => ‘Index’,
      ‘action’ => ‘index’,

  6. Jamal said, on January 10, 2014 at 6:51 pm

    You can add a optional trailling slash in each route:

    'child_routes' => array(
    'default' => array(
    'type' => 'Segment',
    'options' => array(
    'route' => '[/:controller[/:action[/:id]]][/]',
    'constraints' => array(
    'controller' => '[a-zA-Z]+',
    'action' => '[a-zA-Z]+',
    'id' => '[0-9]+',
    'defaults' => array(

    • samsonasik said, on January 11, 2014 at 1:01 am


    • Szerémi Attila (@amcsi_san) said, on April 22, 2014 at 5:07 pm

      This does allow trailing slashes which is great, but it also undesirably places trailing slashes at the end of URLs made with the `urlFor()` and similar helpers, which is bad.

      • samsonasik said, on April 23, 2014 at 4:42 am

        instead of saying bad, you better contribute to the project to make it better🙂

      • Szerémi Attila (@amcsi_san) said, on April 29, 2014 at 5:31 pm

        Or you could use

        ‘type’ => ‘Segment’,
        ‘options’ => array(
        ‘route’ => ‘/[:controller[/:action]][:trailingSlash]’,
        ‘constraints’ => array(
        ‘controller’ => ‘[a-zA-Z][a-zA-Z0-9_-]*’,
        ‘action’ => ‘[a-zA-Z][a-zA-Z0-9_-]*’,
        ‘trailingSlash’ => ‘/’,
        ‘defaults’ => array(
        ‘controller’ => ‘Index’,
        ‘action’ => ‘index’,

        And `urlFor()` will not add the trailing slash. It’s still a bit hacky, but seems like the best solution to me.

  7. Zane said, on April 29, 2014 at 11:44 pm

    I don’t know if it’s just me or if everyone
    else encountering problems with your website.
    It appears like some of the written text within your posts are running off the screen.
    Can somebody else please provide feedback and let me know if this is happening to them as well?

    This could be a problem with my web browser because I’ve had this happen previously.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: