Search code examples
yii2yii2-basic-appyii-url-manager

Url Manager is not working In my yii2 applicatoin


I'm trying to set Url Manager in my yii2 basic template. Below is .htaccess file which is locate mysite.loc/web/.htaccess

Options +FollowSymLinks IndexIgnore

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

and inside my web.php file I've added this code snippet:

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

When I'm trying to enter (For example) mysite.loc/index.php/site/movies It comes error like this: 404 not found error nginx

If anybody knows share me please. What's wrong with my settings???


Solution

  • I've read several topics and have found correct answer. There is a difference between apache configuration and nginx configuration on setting urlManager.

    If Your server Apache (as Vitaly said above):

    .htaccess :

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule . index.php
    

    config/web.php :

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

    If your server is Nginx:

    nginx congfig file (in my case /etc/ngnix/site-enabled/mysite.loc) :

    server {
        listen 80;
        root /var/www/html/mysite.loc/web;
        server_name mysite.loc www.mysite.loc;
        index index.php index.html;
    
        location / {            
            try_files $uri $uri/ /index.php?$args;
        }
    
        location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    

    }

    As you can see I have added this snippet of code and nothing more :

    location / {            
            try_files $uri $uri/ /index.php?$args;
    }
    

    config/web.php file is same as Apache server :

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