How to remove controller name from URL in CodeIgniter

In CodeIgniter or any MVC application, the regular URL structure is like example.com/controller/function/parameter which appear as example.com/user/profile/ariful.  But sometimes we may want to omit the controller name here to make the url more beautiful like example.com/profile/ariful or even example.com/ariful as best url.

This beautiful url can be achieve through the URI Routing of CodIgniter. In
For example.com/ariful, use this code

 application/config/route.php

application/config/route.php file, you’ve to write following code for example.com/profile/ariful

  $route['user/(:any)']             = "user/profile/$1";

For example.com/ariful, use this code

  $route['(:any)']                  = "user/profile/$1";

If you have multiple parameters like

public function profile($username, $city)
{
     echo "You are ".$username." from ".$city;
}

Your route will be

5
  $route['(:any)/(:any)']                  = "user/profile/$1/$2";

If you want to use number in parameter instead of string like these examples, you need to use (:num) instead of (:any).

If you are looking for something like modifying URL, I hope this post may help you. If you’ve any further question, you can comment bellow.

Collection of My Web Development Assets

This is a short story of why I initiated the GitHub repo  “Frameworks Library Plugins I use in work”

I am developing web sites and web applications from 2011. Though I was working as a programmer for long time, I drive into web technologies after staring freelancing and outsourcing through oDesk.

My evolution is something like WordPress > CodeIgniter > Laravel > AngularJs > Nodejs. I started my outsourcing career with small WordPress thick and Installation, Theme modification and ended with WordPress theme development. Then eventually moved to Web Application development using CodeIgniter, Laravel and Angularjs. Though these are only the lading Frameworks, I used many other library, plugins, scripts etc. But most of time I keep forgetting which library or plugin or script I used in last projects and needed to research those. I considered this as a time killing process.

To solve this issue, I though to initiate and maintain a list of Frameworks, Library, Plugin, Scripts I used. It will be easy for me to track my usages and might be helpful for others too. So I initiated GitHub repo named “Frameworks Library Plugins I use in work”.  This is not I’ve listed all things I used in past years, of-course I’ve forgot many of them but I am intended to keep it filling.

If you like the repo, you can fork it and continue as your repo as well.

Philosophy of Important Things To Do

I’ve been a stargazer from my childhood. In any quite place with dark night and multi billions of stars in sky is the all best moments I’ve ever passed in my life.

My wife found this photo in 9Gag and shared in my facebook wall. The quote means a lot to me and though to share in my blog to keep it close to me.

“If people looked at the stars each night, they’d  live a lot differently – when you look into infinity, you realize that there are more important things than what people do all day.”

philosophy of important things to do
Source: 9gag

Some people look before they cross the road

Found this random quote in Facebook and fill to preserve it to keep me motivating toward the science and prosperity and keep me away from superstation.

“I have noticed even people who claim everything is predestined, and that we can do nothing to change it, look before they cross the road.” – Stephen Hawking

stephen hawking quote
Disclaimer: I didn’t search for any direct reference for this quote, but I like the saying even if this is not said by Stephen Hawking

Remove index.php from URL to avoid duplicate content issue

You want to redirect example.com/index.php/welcome to example.com/welcome ? Are you tired of finding proper solution about how to Remove index.php from URL to avoid duplicate content issue  or to redirect index.php to actual URL without index.php? You are in the right place mate!

I am a PHP based developer and most of time I use CodeIgniter as my php framework. Those who use CodeIgniter and obviously other framework, we use some technique to remove index.php  from our URL to make it more clean, short , Web 2.0 friendly and penitentially to avoid duplicate content issue in Google Web Master. In CodeIgniter, we do it in two steps:

How to remove index.php from URL in CodeIgniter

Step 1: We update application/config/config.php file

//keep the variable empty to remove index.php after your domain name
$config['index_page'] = '';

Step 2: We update our .htaccess file as

RewriteEngine on
Options -Indexes
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Cool! That removes index.php from our URL.. thats really great… but wait! What if anyone put index.php in your url like http://www.example.com/index.php/welcome/ ? Yes that is still accessible and duplicate content of http://www.example.com/welcome/ .

How I removed the index.php from url

I had the same problem in one of my project and needed to solve it. I spend hours to solve it but couldn’t found any solution until I found this post htaccess remove index.php from url in stackoverflow. I just put following lines in my .htaccess file

RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

and my problem solved.

Now if you intentionally write http://www.example.com/index.php/welcome/ in your address bar, it will be converted to http://www.example.com/welcome/ . I guess your problem is solved to.

Let me know if it helps you

 

CodeIgniter .htaccess and index.php issues – Solution

CodeIgniter index.php and .htaccess issue ruined my last night. I’ve solved this simple issue many times before but not any solution was working that time.

Few days before today, I was working in a client project which I’ve been demonstrating to my client from my personal demo server which is a HostGator Baby Plan Hosting.  The site was running fine in my local pc (Win7 running XAMPP 1.7.7 running the site in VirtualHost) and in my demo server but when I push the code in client’s staging server (Rackspace Cloud server with CentOS 6.4, Apache2 ) , I was getting a 404 Page Not Found error message. First I checked my .htaccess file and it was same as my demo server, same as my local pc which is

RewriteEngine on
Options -Indexes
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

In my config.php file, I set

$config['base_url'] = 'http://staging.example.com/';
$config['index_page'] = '';

but nothing was working. I tried every solution I used before and I found over Internet but couldn’t find any perfect solution to solve my problem.

Everything seems fine and I was getting frustrated. Finally one solution I found where it says, I’ve to edit my httpd.conf file

httpd.conf File location in CentOS 6.4
httpd.conf File location in CentOS 6.4

and change AllowOverride None to AllowOverride All and saintly this solution worked Perfectly.

VirtualHost settings
VirtualHost settings to remove index.php file in CentOS 6.4

What Problem this was?

With my little understanding on server things, I understand that though we set the rule in .hetaccess to remove the index.php file, it was not fully permitted by staging.conf file. When working in Windows with XAMPP, this file location is in your drive:\xampp\apache\conf\extra\httpd-vhosts.conf

If you still reading, let me know if the solution work for you or If I need to update this posting in anywhere. I am open to creative suggestions :)

How to get value of Array in foreach loop

While working on my CodeIgniter based Open Source Project Management tool “Koronio”, suddenly I needed create a loop to generate a Dropdown element with array key as option value. This is very simple trick, provably everyone knows it, though I felt to share it.

Lets say, we have a php array named $priority which has 5 items.

$priority=array('Low','Normal Low',"Normal",'Normal High','High');

We want to show this items in a Select Item with Array value as option name and Array key as option value.

foreach($priority as $key=> $val)
{
echo '<option value="'.$key.'">'.$val.'</option>';
}

Thats It! You’ll have a dropdown list with the Priority list and value as option value.

Check Password Strength with jQuery Plugin

I was developing an Web Application recently. Like all other web app, this has user registration process and  password field for user. My client wanted to use a Password Strength Meter to indicate user how strong is his/her password. Well that’s a nice and must have feature for most of apps now.

I been searching for a jQuery plugin for this password strength feature and found a lots of plugins available for use. Well, I can’t use a lot of plugins but just one and I had to pick only one and I picked a simple one.

I choose this jQuery Password Strength Plugin from many. Reason for this choosing is,

  • easy to use
  • small but good documentation
  • little but necessary customization options

How to use Query Password Strength Plugin

Requirements

  • jQuery
  • jquery.passstrength.js or jquery.passstrength.min.js

Download jquery.passstrength.js or jquery.passstrength.min.js from original site and keep them in your root folder or any folder you want (figure1)

Files for password strength meeter
Figure 1

Assume following is your html for password field in index.html (figure2)

<div id="passwordmeter">
  <label for="password">Password</label>
  <input type="password" id="password"/>
</div>
password field
Figure 2

Now add the following javascript at bottom of your page (before </body>).

<script type="application/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="application/javascript" src="passstrength.min.js"></script>
<script type="application/javascript">
jQuery(document).ready(function()
{
  jQuery('#password').passStrengthify();
});
</script>

Well, that’s it. This should be working like this now.

Password Strength Meeter
Password Strength Meeter

See the demo here. Download Demo