0

I am facing an issue in my YII2 project. I am adding the URL suffix in the rules and it is throwing me 404 error. I don't know why i am facing this issue. Let me share my url Manager rule

 'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'suffix' => '.html',
        'rules' => [
            'login'=>'userportal/default/login',
        ],
    ],

If i remove suffix or comment that line my web application will run fine. But if with this code the web application throws 404 error.

I am using xampp on my localhost with apache server.

The .htaccess in web folder looks like this

RewriteEngine on
RewriteRule ^index.php/ - [L,R=404]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

Do let me know what is wrong?

Thanks

kool_Coder
  • 149
  • 1
  • 12

1 Answers1

0

Try this:(Working solution)

Basic app

config/web.php

'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'suffix' => '.html',
            'rules' => [
                '<action:\w+>' => 'site/<action>',
                '<action:\w+>/<id:\d+>' => 'site/<action>/<id>',
            ],
        ],

app/web/.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

To access your action you simply use login.html and it will render site/login action. To be able to properly render your url, use Url helper like this:

Url::toRoute('site/login')

it will generate proper link using the desired extension:

login.html

Also here is the documentation with more tricks on how to manage url's in yii2 application.Routing and URL Creation

Serghei Leonenco
  • 3,478
  • 2
  • 8
  • 16