URL Management in Yii

Having a long url is seo unfriendly so mechanisms are devised to format the url in much cleaner and precise way. One of the best way to achieve this is using .htaccess file to rewrite the urls.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

This script will hide .php extension of the url and provides much cleaner url. For instance: http://ujjwalprajapati.com.np/cleanurl.php would look like http://ujjwalprajapati.com.np/cleanurl

For a url with parameters like http://ujjwalprajapti.com.np/index.php?id=10, it would be much clean to use it like http://ujjwalprajapti.com.np/10

RewriteRule ^(.*)$ index.php?id=$1 [L]

 

In Yii, URL is managed by CUrlManager. To format the URL, we should configure the urlManager application component so that createUrl can automatically switch to the new format and the application can properly understand the new URLs.

array(
    ......
    'components'=>array(
        ......
        'urlManager'=>array(
            'urlFormat'=>'path',
        ),
    ),
);

This will format the url like: http://ujjwalprajapati.com.np/index.php?r=post/read&id=10 to http://ujjwalprajapati.com.np/index.php/post/read/id/10.

Now, we can hide index.php configuring the property showScriptName.

array(
    ......
    'components'=>array(
        ......
        'urlManager'=>array(	
            'showScriptName' => false,
            'urlFormat'=>'path',
        ),
    ),
);

And also, we need to use .htaccess for the purpose.

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

We can also use named parameter in url routing to route specific format url to specific controller/action.

 array(
    ......
    'components'=>array(
        ......
        'urlManager'=>array(
           'showScriptName' => false,
           'urlFormat'=>'path',
           'rules'=>array(
       	       '<userid:[\w.]+>/<imageid:\d+>' => 'profile/image',
               '<userid:\w+>/friends' => 'profile/friends',	
               'message/<userid:[\w.]+>' => 'message/view',
               '<userid:[\w.]+>/<action:\w+>/<id:\d+>' => 'profile/<action>',  
           ), 
        ),
    ),
);
  • The first rule will be implied when calling $this -> createUrl(‘ujjwal/10’), here ‘ujjwal’ is the userid and ‘10’ is the imageid. This will call upon the action imageof the Profile Controller class.
  • The second rule has fixed action. Calling $this -> createUrl(‘ujjwal/friends’) will call upon action friends of Profile Controller class.
  • The third rule has fixed controller. Calling $this -> createUrl(‘message/ujjwal’) will call upon view action of the Message Controller class.
  • The forth rule is implied calling $this -> createUrl(‘ujjwal/message/10’), this will call upon the action message of the Profile Controller Class which is fixed and id 10 is passed as parameter to action specified.

Leave a Reply

Your email address will not be published. Required fields are marked *

* Copy This Password *

* Type Or Paste Password Here *

www.000webhost.com