yii2yii2-basic-appyii-routing

Yii2 rules in routing section


I am new to Yii2 and currently, I am receiving an error while setting up the application. I try to use friendly URL.

According to the DOCS

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'enableStrictParsing' => false,
    'rules' => [
        //... rules ...
    ]
]

I set this section in my application as:

'urlManager' => [
    'class' => 'yii\web\UrlManager',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'enableStrictParsing' => false,
    'rules' => [
        '/' => 'site/index',
        '/entry' => 'site/entry',
        'defaultRoute' => 'main/index'
    ]
]

This worked fine when I try to open URL http://yii2/entry.

If i set as:

'urlManager' => [
    'class' => 'yii\web\UrlManager',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'enableStrictParsing' => false,
    'rules' => [
        '/' => 'site/index',
        '/entry' => 'site/entry',
        'defaultRoute' => 'main/index'
    ]
]

If I try to open URL http://yii2/site/entry I received error 404.

Also, if i set:

'urlManager' => [
    'class' => 'yii\web\UrlManager',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'enableStrictParsing' => false,
    'rules' => [
        '/' => 'site/index',
        '<controller>/<action>' => '<controller>/<action>',
        '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        'defaultRoute' => 'main/index'
    ]
]

If i try to open url http://yii2/site/entry I received error 404 too.

Why is it happening? Where I can read more detail about routing in yii2? Where is my error?

UPD: Apache2.2 (win) .htaccess contain:

RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# if $showScriptName is false in UrlManager, do not allow accessing URLs with script name
RewriteRule ^index.php/ - [L,R=404]

UPD2: i receive 2 variant of 404 error: when try to use routing rule:

enter image description here and when i'm using url not in rules array: enter image description here


Solution

  • Modify your rewrite rules.

    Move line

    RewriteRule ^index.php/ - [L,R=404]
    

    to be the first one after RewriteEngine on.

    Or add "last" flag ([L]) to:

    RewriteRule . index.php [L]
    

    Without this rules processing continues to next one.