Codxplore.com

Latest news:

Sahih al-Bukhari (সহীহ বুখারী) is a free Hadith application for android. This application is advertisement free. Download now https://play.google.com/store/apps/details?id=com.akramhossin.bukharisharif

Yii2 Turning on Pretty URLs


Yii2 Turning on Pretty URLs

In this blog post i will discuss about Yii2's pretty URLs.

Yii2 pretty URLs relies on mod_rewrite module on apache or httpd server.

Without Yii2 pretty URLs if you click on any link for example About Us menu,

the URL will be something like http://localhost/hello/web/index.php?r=site/about. 

I'd like to change that to http://localhost/hello/web/site/about.

The config subdirectory includes environment configurations for our web and console applications as well as the database settings. 

Edit /config/web.php to add urlManagement to the current web application. 

Add the following urlManager section within the components array:

'components' => [
  ...
  'urlManager' => [
          'showScriptName' => false,
          'enablePrettyUrl' => true
  ],   
  ...
]
After that, create an .htaccess file into the /web directory where the primary index.php file exists:

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

We need to make sure the mod_rewrite module is running locally; if not we need to enable mod_rewrite  module.

After all changes done now on your browser, visit this URL: http://localhost/hello/web/site/about. 

You should see the Yii application About Us page and clicking on other menu options should show pretty URLs.

Tags : PHP, Yii2,

Views : 1386

Subscribe Us


Follow Us