Archive for Tech Writing

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.

zpanel in port other than 80

Once you installed zpanel, it runs in port 80 by default if you didn’t installed it in sub-domain. This will point your domain to zpanel instead of your application. If you want zpanel and your application co-exist in the same domain without installing zpanel in sub-domain again, then one way around is running the zpanel in port other than 80.

The following configuration is needed in order to enable zpanel in port other than 80. The configuration could be added to httpd.conf file in /etc/httpd/conf/

# Ensure that Apache listens on port 10001
Listen 10001

# Listen for virtual host requests on all IP addresses
NameVirtualHost *:10001

<VirtualHost *:10001>
DocumentRoot /etc/zpanel/panel
ServerName www.mysite.com

# Other directives here
</VirtualHost>

This configuration is useful not only to allow zpanel to run in port mentioned but using this configuration we can host different application in different port.

Understanding group file in Linux

/etc/group is a text file which defines the groups to which users belongs to under Linux and UNIX operating system. There is one entry per line, and each entry is separated by colon(:) symbol. There are total of 4 fields per line.

rootuser:x:513:ujjwal,chris,mas

Now, let’s elaborate the fields for the entries.

  • Group Name: Name of the group.
  • Password: Generally password is not used, hence it is empty/blank. It can store encrypted password. This is useful to implement privileged groups.
  • Group ID: Each user must be assigned a group ID. You can see this number in your /etc/passwd file.
  • Group List: It is a list of user names of users who are members of the group. The user names, must be separated by commas.
www.000webhost.com