Usually, in the CodeIgniter 4 project, you will see there is an additional argument in the URL. That is index.php in your project URL. So, if you don’t want this extra parameter then you can remove it. This will make a clean URL of your website. There are several ways to remove this index.php in CodeIgniter 4. You can create a .htaccess file and create the rule to override the URL. It will require the host to access the created .htaccess file. So, it depends upon the host like Apache or Nginx. In my last tutorial, I have shared the post on CodeIgniter 4 Multiple Image Upload. If you followed that then you have the idea of redirection of the URL with index.php after image upload. In this post, I am not going to create a new project in CodeIgniter 4 for this. I will be using an existing project here.
Prerequisites
- To remove the index.php in CodeIgniter 4, you must have an installed project. You can create a new one if you don’t have.
I am not going to show you to create a project in CodeIgniter 4 here.
How to Check index.php in URL
Firstly, I have opened the project inside the VS Code editor. I already started my development server by hitting the spark command.
php spark serve
Now, you can see here, the development server is started on http//localhost:8080
.
Now, I am diverting you to the browser by following the above URL.
So, I already have the image upload functionality in my last post. Now, I have navigated to the specified route and I have the below screen.
Firstly, let me show you the impact of index.php before removing from the URL in CodeIgniter 4. Here, I have already the validation rule for validating image before upload. So, I chosen the valid image and clicked on the Upload button.
Here, the image is uploaded successfully. Now, you can see the URL has been redirected with index.php by default. The below URL is http://localhost:8080/index.php/upload
.
Here, this is the impact of having the index.php in the URL. So, let’s remove it in the simple steps.
Form Validation Example in Codeigniter 4 For Beginners
Remove index.php in CodeIgniter 4
Now, come back to the CodeIgniter 4 project. Firstly, go to the app/Config/App.php. You will see the baseURL of the project.
If you want to change the base url of the CodeIgniter 4 project then you can change it from here.
In the next line, you will see the Index File description. So, you already noticed one thing while accessing the homepage of the CodeIgniter. In the indexPage property by default the index.php was added.
Here, I haven’t changed the base URL. I will access it on the default port by running the applicaton through the spark command. So, I just removed the index.php from the indexPage.
CodeIgniter 4 project has by default .htaccess file in the public folder. So, no need to create another .htaccess file.
Create a CRUD Application in CodeIgniter 4 For Beginners
Now, go back to the homepage and refresh the URL. Here, I am trying to upload the image again. In the result, you can see the image is uploaded and the index.php is not showing in the URL.
How to Upload Image and File in Codeigniter 4 with Validation
Conclusion
Finally, we removed the index.php from the URL of CodeIgniter 4 application. Now, the URL became neat and clean. So, you can create routes and everything will work perfectly. I hope this tutorial will help you.
Ram says
Hi,
Your posts are really helpful to me. Thank you…. I’m RAM. I’m learning Codeigniter. I have an issue when using redirect function in filter. It leads like The page isn’t redirecting properly. How to solve this issue. Could you help me with this.
This is my code:
namespace App\Filters;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;
class MyFilter implements FilterInterface
{
public function before(RequestInterface $request, $arguments = null)
{
if (! isset($_SESSION[‘isLoggedIn’]))
{
return redirect()->to(site_url(‘login’));
}
}
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
// Do something here
}
}
This is the error I got:
The page isn’t redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
This problem can sometimes be caused by disabling or refusing to accept cookies.
Thank you.
santhosh says
Thank You For this help for remove the index.php from url after submitting the form .Thank you
Arun Bhatiya says
Thanks for sharing this article, It helped me a lot.