Friday, May 18, 2012

nginx rewrite rule for codeigniter

This is my nginx configuration file, which obtain the assist from http://www.farinspace.com/codeigniter-nginx-rewrite-rules/

Here are the few things you MUST take care:
1. All rewrite rule & root must set in server scope, NO location \{} nor ~\.php${} !!!
2. In codeigniter application/config/config.php file

  • set $config[‘index_page’] = “”;
  • set $config[‘uri_protocol’] = “REQUEST_URI”;

Here is the rewrite rule that you need

    #page is my default controller, you will need to alter it to yours       if ($request_uri ~* ^(/page(/index)?|/index(.php)?)/?$)    {        rewrite ^(.*)$ / permanent;    }    # removes trailing "index" from all controllers    if ($request_uri ~* index/?$)    {        rewrite ^/(.*)/index/?$ /$1 permanent;    }    # removes trailing slashes (prevents SEO duplicate content issues)    if (!-d $request_filename)    {        rewrite ^/(.+)/$ /$1 permanent;    }    # removes access to "system" folder, moreover allows a "System.php" controller    if ($request_uri ~* ^/system)    {        rewrite ^/(.*)$ /index.php?/$1 last;        break;    }    # unless the request is for a valid file (image, js, css, etc.), send to bootstrap    if (!-e $request_filename)    {        rewrite ^/(.*)$ /index.php?/$1 last;        break;    }

For more nginx rewrite rule, please visit http://www.x-note.co.uk/category/nginx/

No comments:

Post a Comment