Yii2:-Pretty URL's are formed, but not working (says 404 “NOT FOUND”)

房东的猫 提交于 2019-12-03 07:54:36

Finally I made it working:-

1. created two .htaccess file (one on root and one in web folder of my application):-

root .htaccess:-

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On
</IfModule>

<IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_URI} ^/.*
    RewriteRule ^(.*)$ web/$1 [L]

    RewriteCond %{REQUEST_URI} !^/web/
    RewriteCond %{REQUEST_FILENAME} !-f [OR]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^.*$ web/index.php
</IfModule> 

web folder .htaccess:-

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

2.In config/web.php:-

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

3. Changed in (apache2.conf) like below:-

<Directory /var/www/html/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

4.Then run below commands:-

a. sudo /etc/init.d/apache2 stop (to stop apache)

b. sudo killall apache2 (to kill process and to check that no process is running)

c. sudo netstat -l|grep www (to check port 80 is not in use right now)

d. sudo /etc/init.d/apache2 restart (restart apache)

And now everything worked fine.

By the Way Thanks to every-one who tried to help me out.

Reference taken:-

https://www.my-yii.com/forum/topic/how-to-set-up-pretty-urls-in-yii2-basic-template

https://askubuntu.com/questions/48362/how-to-enable-mod-rewrite-in-apache

Why dont you just give the rules in your web.php file? like below:

'urlManager' => [
        'class' => 'yii\web\UrlManager',
        // Disable index.php
        'showScriptName' => false,
        // Disable r= routes
        'enablePrettyUrl' => true,
        'rules' => array(
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        ),
    ],

The rules i set here is only an example, you can set it the way you want your url to look like.

EDIT: If its not still working, try to set a virtualhost instead with:

<Directory "/var/www/html/yii2/web/">
    DirectoryIndex index.php
    AllowOverride All
    Order allow,deny
    Allow from all
    Allow from 127.0.0.1 localhost
</Directory>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!