Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 730
Anant Kumar Singh Avatar asked Dec 20 '16 10:12

Anant Kumar Singh


1 Answers

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>
like image 132
Francis Ngueukam Avatar answered Oct 05 '22 16:10

Francis Ngueukam