0

After uncomment enablePrettyUrl get 404 error

For example:

The requested URL /site/index was not found on this server.

Apache/2.4.7 (Ubuntu) Server at yiibasic.com Port 80

When I comment it again, everything is working

'components' => [
    'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
        ],
    ],
]

Can't understand what the problem is.
.htaccess:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php
2
  • Please add Apache configuration and logs. Commented Nov 30, 2016 at 18:39
  • question was edited Commented Nov 30, 2016 at 19:49

2 Answers 2

0

The (Yii2) URL manager supports two URL formats:

the default URL format; the pretty URL format.

By default 'enablePrettyUrl' is set to false this mean that the url have the format

your/index.php?r=post%2Fview&id=100

If you uncomment 'enablePrettyUrl' => true,

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

you enable the format for prettyUrl like:

/index.php/post/100

like as you easy see the two format are different so an explict call based on a format return a 404 error .. when you change the urlManager format enabling or disabling pretty url ..

you can call the url avoiding this problem using urlHelper

 use yii\helpers\Url;
 echo Url::to(['post/view', 'id' => 100]);

with this helper Yii2 create the proper url according on the pretty url enabling state in use

You can find a brief guide on yii2 routing here http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html

Sign up to request clarification or add additional context in comments.

2 Comments

Where I should use this helper? In config or in each file, clarify please
The Url::to(9 must be used in each call for avoid the error if you change the prettyUrl status .. i have added a link in answer for a brief guide to yii2 routing
0

After adding .htaccess file in your ./web folder (if using basic template ), you must then add some rules in the urlManager section of your web.php config file...see below

'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => false,
            'rules' => array(
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            ),
        ],

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.