Question

Well, PHP times.

My client wants me to use Yii2 as the framework for his project.

I got it up and running. No problem. I used the advanced template via composer.

Set my web root to /frontend/web, etc.

NOW, i want to use this url format

website.com/messages/ or website.com/messages/tom... etc.

Right now the way is setup shows website.com/index.php?r=messages/index...

I found this documentation...

https://github.com/yiisoft/yii2/blob/master/docs/guide/url.md

But i can't seem to get it straight.

Here are my steps...

I configured my apache server to point to /usr/www/payroll/frontend/web/

I added to my web folder a .htaccess file with this content.

RewriteEngine on

# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php

I also added the component 'urlManager' as in the directions. It seems to catch the request and modify it.

'components'=>[
    'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName' => 'false'
        ],
],

For example if I type website.com you can see it adds /site/index to the url. (without the url component active it simply adds /index.php?site/index)

So, obviously there's a modification perfomed to the url (via UrlManager) but I get 404 error

I am running out of ideas here. I am new to Php, Apache and Yii2. Any help, Greatly appreciated.

Thanks

Was it helpful?

Solution 3

Ok, here is the solution.

In recents version of Apache (from 2.3.9), AllowOverride is set to NONE by default. Previous versions have AllowOverride set to ALL.

Yii2 assumes that AllowOverride will be set to ALL.

If you want to read the whole thread at Yii Forum, here is the link

http://www.yiiframework.com/forum/index.php/topic/53295-url-manager-for-seo-friendly-url-404-error-code/

Thank you for your help and messages!

OTHER TIPS

To make pretty URL working on Yii 2.0 you need 2 things:

1: Edit /frontend/config/main.php (or the appropriate main config in your case) and add:

'components'=>[
        'urlManager' => [
                'class' => 'yii\web\UrlManager',
                'enablePrettyUrl' => true,
                'showScriptName' => false,
            ],
    ],

2: Add a .htaccess file in YOUR WEB ROOT folder. In yii 2.0 advanced this is NOT project root directory but instead: /frontend/web

.htaccess example:

RewriteEngine on

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

RewriteRule . index.php

just change 'showScriptName' => 'false' to 'showScriptName' => false and it will work

For Yii2 basic, I just added the codes below to /myappfolder/config/web.php:

'urlManager' => [

'class' => 'yii\web\UrlManager',

'enablePrettyUrl' => true,

'showScriptName'=>false,

]

Also, added .htaccess in /myappfolder/web/ :

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php

This works well for me. Hope this helps others who have same problem. : )

If you want to use like this http://domain.com/controller_name/action_name , you only need to enable pretty url in your config file :

'components'=>[
    'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName' => 'false'
        ],
],

Now you can use urls as you wish `website.com/messages/ or website.com/messages/tom.

If you want to use query string in your url this is how it works now in Yii 2 website.com/message?id=12

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top