EE Logo Twitter Logo Facebook Logo Linked In Logo

April 11, 2011

SEO Friendly URLS in CodeIgniter 2.0 + HMVC


Many of you SEO fanatics may have realized that CodeIgniter is not very SEO-friendly out of the box. It auto generates URLs based on routes which are made up of a controller name and method name. For URLs to be considered SEO-friendly and be universally recognized by search engines, keywords must be separated by a dash. Many of you are thinking you will just go rename your controllers and methods with dashes. That would be nice and easy, but this is not a legal syntax and PHP will throw errors. There is the option to manually enter each route in your routes.php file, but this could be a serious beat down and come back to haunt you later on down the road.

Here is a quick guide to get your CodeIgniter 2.0 + HMVC (Modular Extensions) setup supporting SEO friendly URLs:

Because of the way CodeIgniter functions, you will not have to make any changes to your CodeIgniter or HMVC core files. When you installed HMVC you should have added a MY_Router.php file to the /application/core/ directory. By replacing that file with the following code you should now be able to reference controllers and methods with dashes. For example a controller with the name my_controller can can now be referenced as my-controller in your URL.

<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
// load the MX_Router class
require APPPATH."third_party/MX/Router.php";                                                                             

class MY_Router extends MX_Router {                                                                                      

    private $module;                                                                                                     

    public function fetch_module() {
        return $this->module;
    }                                                                                                           

    public function _validate_request($segments) {                                                                       

        /* locate module controller */
        if ($located = $this->locate($segments)) return $located;                                                        

        /* use a default 404 controller */
        if (isset($this->routes['404']) AND $segments = explode('/', $this->routes['404'])) {
            if ($located = $this->locate($segments)) return $located;
        }                                                                                                                

        /* use a default 404_override controller CI 2.0 */
        if (isset($this->routes['404_override']) AND $segments = explode('/', $this->routes['404_override'])) {
            if ($located = $this->locate($segments)) return $located;
        }                                                                                                                

        /* no controller found */
        show_404();
    }                                                                                                                    

    /** Locate the controller **/
    public function locate($segments) {                                                                                  

        $this->module = '';
        $this->directory = '';
        $ext = $this->config->item('controller_suffix').EXT;                                                             

        /* use module route if available */
        if (isset($segments[0]) AND $routes = Modules::parse_routes($segments[0], implode('/', $segments)) {
            $segments = $routes;
        }                                                                                                                

        /* get the segments array elements */
        list($module, $directory, $controller) = array_pad($segments, 3, NULL);                                          

        $module = str_replace('-', '_', $module);                                                                                                                                                                                

        $controller = str_replace('-', '_', $controller);                                                                                                    

        $directory = str_replace('-', '_', $directory);                                                                  

        foreach (Modules::$locations as $location => $offset) {                                                          

            /* module exists? */
            if (is_dir($source = $location.$module.'/controllers/')) {                                                   

                $this->module = $module;
                $this->directory = $offset.$module.'/controllers/';                                                      

                /* module sub-controller exists? */
                if($directory AND is_file($source.$directory.$ext)) {
                    return array_slice($segments, 1);
                }                                                                                                        

                /* module sub-directory exists? */
                if($directory AND is_dir($module_subdir = $source.$directory.'/')) {                                     

                    $this->directory .= $directory.'/';                                                                  

                    /* module sub-directory controller exists? */
                    if(is_file($module_subdir.$directory.$ext)) {
                        return array_slice($segments, 1);
                    }                                                                                                    

                    /* module sub-directory sub-controller exists? */
                    if($controller AND is_file($module_subdir.$controller.$ext))    {
                        return array_slice($segments, 2);
                    }
                }                                                                                                        

                /* module controller exists? */
                if(is_file($source.$module.$ext)) {
                    return $segments;
                }
            }
        }                                                                                                                

        /* application controller exists? */
        if(is_file(APPPATH.'controllers/'.$module.$ext)) {
            return $segments;
        }                                                                                                                

        /* application sub-directory controller exists? */
        if(is_file(APPPATH.'controllers/'.$module.'/'.$directory.$ext)) {
            $this->directory = $module.'/';
            return array_slice($segments, 1);
        }                                                                                                                

        /* application sub-directory default controller exists? */
        if(is_file(APPPATH.'controllers/'.$module.'/'.$this->default_controller.$ext)) {
            $this->directory = $module.'/';
            return array($this->default_controller);
        }
    }                                                                                                                 

    public function set_class($class)
    {
        $this->class = str_replace('-', '_', $this->class).$this->config->item('controller_suffix');
    }                                                                                                                 

    public function set_method($method)
    {
        $this->method = str_replace('-', '_', $this->method);
    }
}

September 21, 2010

Before Search Engine Optimization Can Start


There is a lot of talk about Search Engine Optimization (SEO) and there is an expectation that every website will be searchable by Google right away. I have heard SEO described as mysterious or voodoo-like. The simple truth is that before an SEO project can start, your web site has to be developed in a manner that is Search Engine Friendly (ready for SEO). There is no mystery or voodoo involved – as a matter of fact, Google tells you exactly what is needed for a Search Engine Friendly site and it is a good standard coding practice.

From Google’s Search Engine Optimization Starter Guide, here are the six most important items that are required for your website to be built as Search Engine Friendly:

Read more…

August 5, 2010

Search Engine Truths


Frequently people will ask us how they can improve their website rankings in search engines. Over the past four years, we have developed the following list of suggestions.

  • Use unique and meaningful title information on every page. When title information is repetitive, the search engine assumes that the pages are too. The same is true for meta description.
  • Include your listing on relevant online directories such as Yellow Pages, Yahoo, Yelp, local service directories or professional groups.
  • Remove technology like frames and excessive graphics, especially if they include text. Text should stand on its own as HTML. Otherwise, it is guaranteed not be seen by search engines.
  • Keep a good balance between text and graphics. Search engines will ignore your graphics and focus on your text, but actual visitors will respond to both.
  • Keep your message accurate and current. More frequently updated sites tend to be indexed more. More usable updates satisfy visitors. Look at ways to add to your content. Blogs or regular site updates can be helpful.
  • Look for online communities to participate in. Freely exchanging information and providing expertise about your field will always increase success.
  • Monitor visitor activity and trends. Review activity and change your website to produce the desired results.

March 8, 2010

Welcome to the Einstein’s Eyes Blog


Welcome to the Einstein’s Eyes Blog (Blogstein’s Eyes)!

We would like to thank you for your interest, and to let you know what you can expect in our coming articles.  Being professionals in the web development industry, we would like to share some of our knowledge and experience with the community that has helped us grow and prosper.  We will be posting regular articles about every aspect of web development; from the code, to marketing and Search Engine Optimization (SEO).

So come back again, bring your morning coffee and join us in our pursuit of excellence on the web!

New articles will be starting on Friday, March 12th, 2010!

© 2012 Einstein's Eyes