October 15, 2014

October 15, 2014
In this article, we are going to discuss about How to create a SEO and Clean URLs in Yii PHP Framework. Yii is a modern and robust PHP framework aimed to secure and scalable programming. It is not hardern or more complex compared to the popular PHP frameworks such as CakePHP. It supports all major databases from MySQL to Sqlite. The best is that it is much faster and scalable compared to other frameworks. The latter is what made me reconsider using it over CakePHP.

By default Yii routes all URL requests transparently through the main index.php file and your URLs look like this:

http://example.org/?r=site/page&view=about

Our aim is to make it look:

http://example.org/site/page/view/about

For this purpose follow these steps:

Open your site root protected/config/main.php and uncomment the part about the urlManager so that it looks like this:

'urlManager'=>array(
     'urlFormat'=>'path',
     'showScriptName'=> false,
     'rules'=>array(
           '/'=>'/view',
     ),

),

Add the following lines to your .htaccess (provided you are using Apache or compatible web server):

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

Last you may wish to drop controller / action from the URL. This can be also done by a custom rule in the urlManager array in protected/config/main.php:

'about'=>'site/page/view/about',

This will ensure that by just going to http://example.org/about you will be routed to http://example.org/site/page/view/about.

0 comments:

Post a Comment