Trailing slash

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Trailing slash

David Muir-2
I'm not sure if this is a Zend/Mvc/Router issue or a server config issue, but I noticed this when going through the Rob Allen's zf2 tutorial.

http://zf-tutorial/album   - 200 - works fine
http://zf-tutorial/album/  - 404 - The requested URL could not be matched by routing.

I'm assuming that because it contains a slash, it's matching the optional part of the route :controller[/:action][/:id], and therefore attempting to run an action called "" (empty string). I tried adding public function Action(){}, but that didn't make a difference.

Is the only way to solve this to be to strip the trailing slash via .htaccess, or conversely make the slash required eg: :controller/[:action/][:id/], and automatically append the trailing slash via .htaccess, if the file doesn't exist?
I'm guessing that's the better solution than having the router handle the situation, as it affects stats and SEO stuff (duplicate content). It did take me by surprise though, that the router crapped out here.

Cheers,
David
Reply | Threaded
Open this post in threaded view
|

Re: Trailing slash

bakura
This post has NOT been accepted by the mailing list yet.
This happens because of the way the route is defined :

'route'    => '/album[/:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'album/album',
                        'action'=> 'index',
                    ),

If the URL is zf-tutorial/album, no problem and the router takes the default value for action that is defined (index in this case). On the other hand, if it is zf-tutorial/album/, it expects a parameter called action. No character after the trailing slash doesn't respect the constraint, and therefore the URL can't be mapped to a route.
Reply | Threaded
Open this post in threaded view
|

Re: Trailing slash

weierophinney
Administrator
In reply to this post by David Muir-2
-- David Muir <[hidden email]> wrote
(on Wednesday, 13 June 2012, 10:12 PM +1000):

> I'm not sure if this is a Zend/Mvc/Router issue or a server config issue, but I
> noticed this when going through the Rob Allen's zf2 tutorial.
>
> http://zf-tutorial/album   - 200 - works fine
> http://zf-tutorial/album/  - 404 - The requested URL could not be matched by
> routing.
>
> I'm assuming that because it contains a slash, it's matching the optional part
> of the route :controller[/:action][/:id], and therefore attempting to run an
> action called "" (empty string). I tried adding public function Action(){}, but
> that didn't make a difference.
>
> Is the only way to solve this to be to strip the trailing slash via .htaccess,
> or conversely make the slash required eg: :controller/[:action/][:id/], and
> automatically append the trailing slash via .htaccess, if the file doesn't
> exist?
> I'm guessing that's the better solution than having the router handle the
> situation, as it affects stats and SEO stuff (duplicate content). It did take
> me by surprise though, that the router crapped out here.

You hit the nail on the head when you mentioned the duplicate content
issue. It's typically better not to have two urls respond to the same
thing.

Ideally, you should either have a rule in .htaccess or in PHP that
redirects if the path_info ends in a slash, using a 301 (moved
permanently) status. (Using PHP would be more portable, particularly
when you move to some fastcgi platforms that do not support URL
rewriting as thoroughly as mod_rewrite.)

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc