2

I am currently building a new sub-system for a client that will run on a sub-directory like https://example.com/new-subsystem.

Their existing website was native/hard-coded PHP so I have to create a new folder on the root and put the CodeIgniter 3 Framework there. I configured the base_url and voila! It is working fine.

Now the problem is when I try to create a new method inside the controller, it returns error 404 that it seems like that the server is trying to handle the request literally and not let CodeIgniter handle it.

Example

When I try to access https://example.com/new-subsystem, it is working fine.

But when I try to access https://example.com/new-subsystem/test it will show Error 404 and seems like the server is trying to find another folder instead. Here is the actual route.php, config.php, controller which I believe that the problem is not on these three.

routes.php

$route['default_controller'] = 'lockdown';
$route['test'] = 'lockdown/test';

config.php

$config['base_url'] = 'http://localhost/project-lockdown/tutorials/';

Lockdown.php (Controller)

    class Lockdown extends CI_Controller {
        public function index()
        {
            // This is working by default. It shows on http://localhost/project-lockdown/tutorials/
            echo "Hello World!"
        }
        public function test(){
        // This should show on http://localhost/project-lockdown/tutorials/test
            echo "Hello, this is a test method and it is not working. Error 404 is shown!";
        }
    }

Main Question: How to tell the server that I want CodeIgniter to handle the rest of the request that is thrown under http://localhost/project-lockdown/tutorials/(controller)/(method) and not take it literally.

For now, I am stuck here and still trying to find a solution to it across the internet and can find a thing. I am no .htaccess guy so I think this has something to with the .htaccess magic or something (which I am trying to learn now).

Any help would be appreciated. This problem will not occur if I just throw in CodeIgniter on the root folder.

Thank you in advance.

5
  • 1
    I think you forgot to remove index.php from the URL. So that you need to append index.php in the URL to access your controller/method. Try with - localhost/project-lockdown/tutorials/index.php/(controller)/… Commented May 4, 2020 at 7:44
  • 1
    Hi @AlokMali, fantastic! It worked I never thought of that. Now I think I need .htaccess to handle the removal of the index.php. Thank you! Commented May 4, 2020 at 7:52
  • 1
    All is working now @AlokMali, Cannot thank you enough for that. I now removed the index.php on .htaccess and It is working now! Thanks again man. Can you post it as answer and I will accept it? Commented May 4, 2020 at 7:55
  • 1
    You are most welcome brother. Commented May 4, 2020 at 8:47
  • I have added it as an answer. Commented May 4, 2020 at 8:49

1 Answer 1

1

You need to append index.php in the URL to access your controller/method. Try with -

localhost/project-lockdown/tutorials/index.php/(controller)/

You can remove index.php by rewriting some rules in .htaccess file.

Sign up to request clarification or add additional context in comments.

1 Comment

This solution worked. Funny how I missed this one. I wrote the rules to instead remove the index.php and set the site_url at the config to empty. Working now. Thank you brother.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.