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

3

11

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 created a .htaccess file and put it on the root (it has the 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 over any link on the page, it shows me something like this: http://localhost/yii2/web/site/about (which shows that pretty URLs are made)

But these URLs are not working (says 404 found)

I tried below code links answers also but didn't work for me:

How to access a controller with pretty url in Yii2

Enable clean URL in Yii2

Sarinasarine answered 20/12, 2016 at 10:15 Comment(3)
Try also, after the previous step, to clear your browser's cache or use another browser.Cessionary
thank you for your answer it really helped me <br>however i used only one .htaccess inside my yii application web root and added <Directory "/var/www/html"> AllowOverride All </Directory> <br> in my default website configuration in apache ie: the file "/etc/apache2/sites-available/000-default.conf" <br> ps: if using advanced template you should add the .htaccess file in the frontend web directory and the backend's <br> hope this is helpfulGunzburg
Here is another option: https://mcmap.net/q/294772/-enable-clean-url-in-yii2Enwomb
S
28

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. Made below changes in config/web.php file :-

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

3. Changes needs to be done in apache2.conf file:

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

4. Now run the 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

Sarinasarine answered 20/12, 2016 at 12:48 Comment(0)
M
9

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>
Martine answered 20/12, 2016 at 10:32 Comment(1)
I have got the solution, but thanks for your priceless help. +1.Sarinasarine
Q
1

If non of above answers worked and you are using Apache as your web server on linux,then make sure that rewrite mode is enabled in Apache and if it's not you can enable it with this command:

sudo a2enmod rewrite 

and after that restart Apache web server

sudo systemctl restart apache2

and it should solve the problem.

Quiddity answered 21/11, 2020 at 8:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.