Pretty url not working in yii2

I started learning yii2 and I tried to make some pretty pretty url stuff but couldn't. What I've done: -

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

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

, then I created the file .htaccessand put it in root (it has the code below):

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

I also opened the apache2.conf file and was modified as follows:

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

I also checked the changes with the command:

 grep -R AllowOverride /etc/apache2

And it looks like this:

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

Now:

when I access my page via:

http: // localhost / yii2 / web /

it opens, and when I find the "About Link" link, it shows me: http: // localhost / yii2 / web / site / about (which shows that a nice handful)

But this URL does not work (says 404)

I tried the following posts for each code:

How to access controller with beautiful url in Yii2

URL- Yii2

+4
2

, : -

1. .htaccess( root - ): -

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> 

- .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. (apache2.conf), : -

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

4. : -

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

b. sudo killall apache2 ( , )

c. sudo netstat -l|grep www ( 80 )

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

.

, .

: -

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

+7

web.php? :

'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>',
        ),
    ],

, , , , , URL .

EDIT:   , virtualhost :

<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>
+5

Source: https://habr.com/ru/post/1664275/


All Articles