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

Anant Kumar Singh picture Anant Kumar Singh · Dec 20, 2016 · Viewed 12.2k times · Source

I have started learning yii2 and I tried to do pretty URL stuff, but failed. What I did:-

in config/web.php (I have edited below):

'urlManager' => [
        'class' => 'yii\web\UrlManager',
        // Hide index.php
        'showScriptName' => false,
        // Use pretty URLs
        'enablePrettyUrl' => true,
        'rules' => [
        ],

then I have created a .htaccess file and put it on root (it has below code):

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

Also I had opened apache2.conf file and changed like this:

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All <! -- instead of none -->
    Require all granted
</Directory>

Also I checked the changes through the command:

 grep -R AllowOverride /etc/apache2

And it shows like below:

/etc/apache2/apache2.conf:  AllowOverride All  <!-- It is showing that done -->

Now:

when I access my page through:

http://localhost/yii2/web/

it's opened and when I hover on any link of the page,it showed me something like this: http://localhost/yii2/web/site/about (which shows that pretty URL's maid)

But these URL's are not working (says 404 found)

I have tried below posts code also, but didn't worked for me:

How to access a controller with pretty url in Yii2

Enable clean URL in Yii2

Answer

Anant Kumar Singh picture Anant Kumar Singh · Dec 20, 2016

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. config/web.php changes:-

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

3. apache2.conf changes:

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

4. Now run below commands:-

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

b. sudo killall apache2 (to kill process)

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

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

And now everything worked fine.

My sincere 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