50 responses to “CodeIgniter NGINX Rewrite Rules”

  1. james

    since you have the catch all line:

    rewrite ^/(.*)$ /index.php?/$1 last;

    then later you have the

    location ~ \.php$ { }

    my question are:

    why the question mark in the end of index.php?

    why only matching address ended with .php? in that case, /index.php?/controller/method/variable won’t be able to translated to /controller/method/variable

    I had to change the lines to the following..

    rewrite ^/(.*)$ /index.php?/$1 last;
    location ~ \.php { }

  2. james

    ok. I take it back what I said above. it turns out the question mark in “rewrite ^/(.*)$ /index.php?/$1 last;” makes “location ~ \.php$ { }” match. so I was wrong :)

  3. Mohammed Islam


    Thank you for this nice article.

    I am looking to redirect robots.txt to robots-off.txt could you tell what is the nginx rule I need for that?

    Thank you!

  4. Bonjovax

    Your Rewrite rule for Nginx is Superb!

    Thanks :)

  5. Alex W

    Thank you so much. This saved hours of my time! Exactly what i was looking for!

  6. Sonu

    How can we redirect URL with .htaccess file.I want to redirect only 1 URL rest will be the same.Please help me….

  7. Sonu

    Hi Dimas,Thanks for your reply.I had try this but it is not working.I am using Codeigniter V1.7 & already rewrite most of the URL’s with .htaccess but unable to redirect URL’s with 301.Do you have any solution for this situation?

  8. Sonu

    Hi Dimas,I can’t show it publicly.My client won’t allow to do so because their privacy hope you understand.But,you can check the below code to know how it looks :

    RewriteEngine On
    RewriteBase /

    I had copied first & last line from that file.Hope it would get some solution!

  9. Sonu

    The first & last line automatically removed.Hope it gives you hint for it IfModule.


  10. Sonu

    Hey Thanks Dimas.This Works! I had recreate complete .htaccess file.It has lots of issues the client has used 2 types of rewrite rules.Might that was causing a problem.

    Thanks for your help!!!

  11. Decibel3276

    Thanks for this excellent post Dimas but I’m having problems. I get a message saying that the “server” directive is not allowed here in /etc/nginx/sites-enabled/000-apps.vhost and if I remove the server directive there I get the same error in /etc/nginx/sites-enabled/000-ispconfig.vhost.

    Any ideas?

  12. Zack

    Thanks .. using this with winginx ;)

    location ~ \.php$ {
    if (!-e $document_root$document_uri){return 404;}
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;

  13. Jimmy

    After hours of searching and scratching my head.. here you are. Thanks, this worked like a charm. Keep up the good work!

  14. Galvin Wolf

    Nginx is very helpful to developer because developer is easy run to his appliafcation by Nginx.That is good information, it is useful to me thanks.

  15. Jimmy

    Great article! After scratching my head for an hour your article helped. But there is one issue, as all the requests are passing in this format “/index.php?/$1″
    It becomes a problem if i am passing some GET parameters using.
    Example if i pass GET param “name” to controller “page” it becomes “/index.php?/page&name=something”

    Any way to solve this issue?

  16. Jim Bowes

    Hey Dimas,

    Great article – I’m just wondering if you’ve ever got Nginx serving Codeigniter and WordPress together successfully?

    I’m trying to do that at the moment we have a folder for Codeigniter files e.g. /ci and one for WordPress /site


  17. abz89

    After somehour googling and testing without any luck..
    Finally found your article!!

    Work like a charm..

    But one question, i’m n00b in nginx
    where should I put the PHPMyadmin block on these??


  18. Jayson


    Am I putting this rewrite rule properly? /etc/nginx/sites-available/default?

    How come I copy-pasted it on the said file and it does not work?

    The URL I am using is : http://ipaddress/proj/index.php?/controller

    Where it should just be: http://ipaddress/proj/controller

  19. Ph1L

    Old post, but still worth a try.
    Using ISPConfig 3 with NGINX, and want to host a Joomla website 1.5, with SEF enabled.

    Howto? Afaik, NGINX ignores .htaccess ?
    I want it to use the .htaccess provided by Joomla.

  20. Ph1L

    Seen it too, but seems like it doesn’t wanna play with ISPConfig 3, grr :)

  21. eric

    “IfIsEvil” is kind of a key to nginx, though. Forcing nginx to evaluate and process many different IF statements is not considered a good practice.

    I’d recommend try_files.

    Check “Front Controller Pattern Based Packages”