How to access controller with pretty url in Yii2

This may be a dumb question, but I can't do it. I searched for this for a long time, but found nothing.

I am using the advanced Yii2 template and I would like to remove "frontend / web /" and "index.php", so it will have a pretty url like: www.mywebsite.com/signin instead of www.mywebsite.com/frontend /web/index.php?r=site/signin

I have this htaccess in the root folder:

Options -Indexes

<IfModule mod_rewrite.c> 
  RewriteEngine on

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

# Deny accessing below extensions
<Files ~ "(.json|.lock|.git)">
Order allow,deny
Deny from all
</Files>

# Deny accessing dot files
RewriteRule (^\.|/\.) - [F]

And also this htacccess in the frontend folder:

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

Options -Indexes

Finally, here is the configuration for UrlManager:

use \yii\web\Request;
$baseUrl = str_replace('/frontend/web', '', (new Request)->getBaseUrl());

return [
    'urlManager' => [
        'baseUrl' => $baseUrl,
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            'signin' => 'site/signin',
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        ]
    ]
]

"Signin" - SiteController.php frontend. ? www.mywebsite.com/signin, www.mywebsite.com/site/signin, . - htaccess config? .

+1

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


All Articles