Enable clean URL in Yii2
First important point is that
Module_Rewrite is enabled on your server(LAMP,WAMP,XAMP..etc) For do URL rewiring in yii2 framework Create one .htaccess file and put in /web 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
Second step
Config folder common/config/main-local.php
add to components array
'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>',
),
],
For me, the problem was:
- Missing .htaccess in the web folder just like mentioned above.
- The AllowOverride directive was set to None, which disabled URL rewrites. I changed it to All and now pretty URLs work nicely.
<Directory "/path/to/the/web/directory/">
Options Indexes
FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
I got it working in yii2. Enable mod_rewrite
for Apache
.
For basic template
do the following:
Create a .htaccess file in web folder and add this
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
Then inside config folder, in web.php add to components
'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>',
),
],
In the case of advanced template
create the .htaccess
files inside backend/web
and frontend/web
folders and add urlManager
component inside common/config/main.php
First, create a .htaccess
at root folder in your Yii2 project with following content:
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]
Create another .htaccess
file in your web folders with following content:
frontend/web/
add backend/web/
Don't forget to add .htaccess
file to both web folders:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
Now It's done. Change your URL configuration in Yii2:
<?php
use \yii\web\Request;
$baseUrl = str_replace('/frontend/web', '', (new Request)->getBaseUrl());
$config = [
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'aiJXeUjj8XjKYIG1gurMMnccRWHvURMq',
'baseUrl' => $baseUrl,
],
"urlManager" => [
'baseUrl' => $baseUrl,
'enablePrettyUrl' => true,
'showScriptName' => false,
"rules" => [
"home" => "site/index",
"about-us" => "site/about",
"contact-us" => "site/contact",
]
]
],
];
return $config;
Your URL will change to:
localhost/yii2project/site/about
=> localhost/yii2project/about-us
localhost/yii2project/site/contact
=> localhost/yii2project/contact-us
localhost/yii2project/site/index
=> localhost/yii2project/home
You can access your admin through
localhost/yii2project/backend/web