How to deal with index.php in CodeIgniter on the Nginx server

Dede Nugroho
2 min readApr 7, 2020

--

Hi, first of all, let me introduce myself, my name is Dede Nugroho you can call me Nugroho :), and I’m from Indonesia. This is my first time writing something online and I want to share with you guys, I’m so sorry if there is any mistake, because my English is pretty limited. So without any further ado let’s begin.

Source of picture: mahfudivanpatoni.blogspot.com

What ‘s CodeIgniter?

CodeIgniter is a PHP framework that loosely based on the popular Model–View–Controller (MVC) development pattern, developed by the British Columbia Institute of Technology. And CodeIgniter is one of my favorite PHP Framework for Web Development, it’s easy to use and had many communities outside there and make learning this framework easy.

What’s Nginx?

Nginx is one of the open-source webserver software and can be used as IMAP/POP3IMAP/POP3 Proxy. Nginx is created by Igor Sysoev in 2002. After 2 Years finally Nginx Released in public. So many people use this webserver because of its performance is really good and stable.and nginx is also my one of favorite webserver it’s fast and as already said above the performance it’s good.

Why are we should remove index.php from our URL?

Actually it’s not mandatory but by removing index.php it’s make our URL prettier. Who doesn't want to make their URL look nice and pretty?
just like:

https://domain.com/index.php/home/

to

https://domain.com/home/

look’s pretty than before right? :D
So how to remove the index.php from your URL in CodeIgniter on the Nginx Server?

if you use apache, you can remove it by adding some .htaccess file in your site directory but .htaccess doesn’t work on the Nginx server, we should edit the configuration to make this work, and I’ll share it with you :)

1. Open your site configuration

First, you need to open your site configuration mine is in /etc/nginx/sites-available/domain.com.conf

2. Add this code into your site configuration

next is you need to add this code into your site configuration domain.com.conf

server {
server_name domain.com www.domain.com;
...
...
location / {
try_files $uri $uri/ /index.php;
}}

in other cases if it’s your CodeIgniter Directory is in another path you can

server {
server_name domain.com www.domain.com;
...
...
location / {
try_files $uri $uri/ /index.php;
}
location /toPath/ {
try_files $uri $uri/ /toPath/index.php
}}

3. Restart you Nginx server

Last is you need to restart your Nginx server to apply the changes

sudo service nginx restart

then done you can access your site without index.php :D

thank you for reading my article,see you in next article :D

--

--