Alan shen
Alan shen

Reputation: 21

How to use CodeIgniter 4.6 in subfolder?

I am trying to create a new site under xampp (php 8.2) with CodeIgniter 4.6(the latest). Here are what I did:

  1. run cmd: composer create-project codeigniter4/appstarter c46 under folder htdocs

  2. update baseURL in App.php like below

    public string $baseURL = 'http://localhost:988/c46/'; // also tried with public at end

Now, I can go to http://localhost:988/c46/public/ to see the default welcome page (the one from Home controller index method). all good so far.

I added a new function in controller Home.php

public function test(): string
{
    return 'ok';
}

Here is my issue, I could not access the page home/test. Seems only the default controller and method is working. I tried:

I also tried to change .htaccess file in public folder RewriteBase to below, both not working

RewriteBase /c46
RewriteBase /c46/public

How can I access the new page/method in controller? what are the changes I need to make to get this work?

Thanks a lot in advance!!

Upvotes: 0

Views: 37

Answers (1)

Franky
Franky

Reputation: 17

I too ran into some problems using the ci4 routing while having the project in a subdirectory, here's what I did:

  1. Change the $baseURL value your app/Config/App.php to http://localhost:988/c46/
  2. Add a .htaccess in the root directory of your project (you'll want to place it in the root of the c46 directory) to rewrite yout http requests from http://project to http://project/public

Here's an example of the .htaccess you can add to your project:

RewriteEngine On

# You might want to enable this, it redirects users from http://url/dir to http://url/dir/
DirectorySlash On

RewriteCond %{REQUEST_URI} !^/c46/public/
RewriteRule .* /c46/public/$0 [L]

DirectoryIndex index.php

Upvotes: 0

Related Questions