NGINX with Kohana 3.x friendly URLs

I really enjoy working with Kohana and since I got a server from one of the hosting companies listed at LowEndBox, I decided to ditch Apache and go with Nginx, thing is… I also like pretty URLs 🙂

As a backup from the comment located here: http://forum.kohanaframework.org/discussion/comment/11553/#Comment_11553

I’m copying and pasting it in my blog for easier reference, plus it’s modified to fit my servers configuration:

location / {
    index    index.php index.html;

    if (!-e $request_filename) {
        rewrite ^/(.*)$ /index.php/$1 last;
    }

    include  /home/httpd/domains/example.com/htaccess;
}

location ~ .php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /home/httpd/domains/example.com/html$fastcgi_script_name;

    include /etc/nginx/fastcgi_params;
}

# kohana
set  $path_info "";
location /index.php {
    if ($uri ~ "^/index.php(/.+)$") {
        set  $path_info  $1;
    }
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /home/httpd/domains/example.com/html/index.php;

    fastcgi_param  PATH_INFO $path_info;
    include /etc/nginx/fastcgi_params;
}

php:// function to clean string for SEO usage

If you need a good function to clean strings to use them as URLs for SEO purposes, here’s a suggestion:

function chunk_special_chars($string) {
  $normalizeChars = array(
    'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A',
    'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I',
    'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U',
    'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a',
    'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i',
    'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u',
    'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f'
  );
  $string = strtr($string, $normalizeChars);
  $string = str_replace('&', '-and-', $string);
  $string = trim(preg_replace('/[^wd_ -]/si', '', $string));//remove all illegal chars
  $string = str_replace(' ', '-', $string);
  $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
  $clean = preg_replace("/[^a-zA-Z0-9/_| -]/", '_', $clean);
  $clean = strtolower(trim($clean, '-'));
  $clean = preg_replace("/[/_| -]+/", '_', $clean);

  return $clean;
} //function

thanks to: sales at mk2solutions dot com for posting their solution @http://us2.php.net/strtr notes

  • IGlr