Sep
How To Generate SEO Friendly URL in Yii Framework
We can configure the URL in Yii php Framework which helps to generate seo friendly url. We can do it by using URL manager in our yii application. In this tutorial, we are going to use Yii2.0
For Basic Template
Remove web/index.php and enable pretty
By Default YII Basic url structure is yiibasic/web/index.php?r=controller/action
So to make url user and SEO friendly we need to make the following changes:
A. Add this in config/web.php in the components array
'urlManager' => [ 'enablePrettyUrl' => true, //'urlFormat'=>'path', 'showScriptName' => false, 'enableStrictParsing' => false, 'rules' => [ // ... ], ],
It will make url like this: yiibasic/web/index.php/controller/action.
Now to remove web/index.php follow Step B and C:
B. Create a file .htaccess on yii installation folder. In this eg. it is yiibasic folder and write 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
C. Copy all data from web folder to site root folder and replace index.php code:
From:
require(__DIR__ . '/../vendor/autoload.php'); require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); $config = require(__DIR__ . '/../config/web.php'); To: require(__DIR__ . '/vendor/autoload.php'); require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php'); $config = require(__DIR__ . '/config/web.php');
Now you can access your yii application using this url :
Yiibasic/controller/action