Codeigniter 4 Remove Public and Index.php From URL

Codeigniter 4 Remove Public and Index.php From URL

To remove public and index.php from URL in Codeigniter 4 framework, you just need to create .htaccess file on the root directory of the codeigniter app and set some configuration on config.php.

In this tutorial, we would love to share with you, how to remove public and index.php from URLs in the new Codeigniter 4 frameworks.

How to remove public index.php from URL in Codeigniter 4

Here are some steps to remove the public index.php from URL in Codeigniter 4 framework with xampp or wamp server:

  • Step 1: Change in App.php File
  • Step 2: Copy index.php and .htaccess
  • Step 3: Change In index.php

Step 1: Change in App.php File

The first step is to open the app.app file. And the changes mentioned below. You have to do this in your App.php file.

So go to project_name/app/Config/App.php and change mentioned below:

public $baseURL = 'http://localhost:8080';

To

public $baseURL = 'http://localhost/your_project_name/';

And the second change in the app.php file:

public $uriProtocol = 'REQUEST_URI';

To

public $uriProtocol = 'PATH_INFO';

Step 2: Copy index.php and .htaccess

Visit inside public directory. And copy index.php and .htaccess to the codeigniter app root directory.

Step 3: Change In index.php

In the root project directory, open index.php and edit the following line:

 $pathsPath = FCPATH . '../app/Config/Paths.php';

 change TO

 $pathsPath = FCPATH . 'app/Config/Paths.php';

If the above solution is not work for; so you can configure your apache server; as shown below:

In the apache server, the mode rewrite is already on. But some default values need to be changed on /etc/apache2/apache2.conf file. Following are changes,

First, find

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>

And change AllowOverride None to All and save.

Then enable mode rewrite using the command(In this case already enabeled),

$ sudo a2enmod rewrite

Then restart the server,

$ sudo /etc/init.d/apache2 restart

Conclusion

In this tutorial, you have learned how to remove the public and index.php in new Codeigniter 4 framework.

Now you can hit the below url in your browser.

example.com/hello

Recommended Codeigniter Posts

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

10 replies to Codeigniter 4 Remove Public and Index.php From URL

  1. Thank you so much now working fine.

  2. nice dear, i was found best solution thank you

  3. i know its work fine but how to use command line after this type of configuration.

  4. Gracias, me sirvio mucho este tutorail, gracias desde México

  5. its working fine thank you.

  6. Very nice post. I just stumbled upon your blog and wished
    to say that I have really enjoyed surfing around your blog posts.
    In any case I will be subscribing to your feed and I hope you write again soon!

  7. where we fine index.php file ?

    • inside public directory of your codeigniter app

  8. Thank you m,an

  9. Thanks for the article mate. It worked smooth as butter for me. Wish I could donate.

Leave a Reply

Your email address will not be published. Required fields are marked *