How to Enable Optional Trailing Slash in Yii2 URLs

By | April 24, 2019

Inspired from Github

If you have links, that might end with slashes (sometimes they are being added automatically), you won’t be able to handle them unless you add a string to your Yii2 project configuration.

Since version 2.0.10 UrlManager can be configured to handle trailing slashes in a simple way. Just make sure your config file has normalizer parameter:

'urlManager' => [
            'showScriptName' => false,
            'enablePrettyUrl' => true,
            'normalizer' => [
                'class' => 'yii\web\UrlNormalizer'

You’re done! Now you can use trailing slashes and everything will be routed correctly.

Leave a Reply

Your email address will not be published. Required fields are marked *