404 Error in CodeIgniter Application After Setting Up Virtual Host on Apache (Ubuntu)

I’m trying to set up my old CodeIgniter-based admin application on a new PC running Ubuntu and Apache. The structure of my application is as follows:

apps
   - api_user
       - application
       - public_html
   - web_admin
       - application
       - public_html

I’ve set up a virtual host for the web_admin part of the application. Here’s my Apache virtual host configuration:

<VirtualHost *:80>
    ServerName admin.app-dev
    DocumentRoot "/var/www/myapp/apps/web_admin/public_html/"
    <Directory "/var/www/myapp/apps/web_admin/public_html/">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog /var/log/apache2/error.log
    LogLevel warn
    CustomLog /var/log/apache2/access.log combined
</VirtualHost>

I’ve also updated the /etc/hosts file:

127.0.0.3    admin.app-dev

The app loads the login page fine, but when I try to submit the login form, it makes a request to http://admin.app-dev/admin_api/login, which results in a 404 Not Found error.

Here’s what I’ve tried so far:

  1. Verified that the mod_rewrite module is enabled.
  2. Checked that the .htaccess file in the public_html directory contains the necessary rewrite rules:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
  1. Ensured that the base URL in the CodeIgniter config.php is set to http://admin.app-dev/.
  2. Verified that the routes.php file has the correct routes for admin_api/login.
  3. Checked the Apache error logs (/var/log/apache2/error.log), but no errors are being logged.
  4. Set the ENVIRONMENT to development in CodeIgniter’s index.php file to enable detailed error reporting, but no additional information appears.

However, when I try to access http://admin.app-dev/index.php/admin_api/login, it works, which suggests that URL rewriting might be the issue. But all necessary rewrite rules seem to be in place.

My environment:

  • OS: Ubuntu 22.04
  • Apache version: 2.4.52
  • PHP version: 8.1
  • CodeIgniter version: 3.x

Additional Information:

  • Permissions and ownership for the public_html directory and files are set to www-data:www-data, with 755 permissions.
  • This same setup worked perfectly on my old development machine.

Question:

Why does the URL http://admin.app-dev/admin_api/login result in a 404 error, and how can I resolve this issue to restore the URL rewriting functionality?

The issue with your CodeIgniter application’s URL rewriting likely stems from a misunderstanding of how CodeIgniter handles URLs with its .htaccess rules. Here’s the breakdown of the problem and how to solve it:

Problem:

The rewrite rule in your .htaccess file rewrites all requests that don’t point to existing files or directories to index.php . However, CodeIgniter expects URLs in the format http://domain/controller/method/parameter1/parameter2/... for routed requests.

Solution:

1. Modify .htaccess Rewrite Rule:

Instead of rewriting everything to index.php , add an additional rewrite rule that specifically handles URLs with a leading segment (like admin_api ). This rule will pass the entire URL string to index.php for processing by CodeIgniter’s routing system.

Here’s the modified .htaccess content:

Apache

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Handle requests for admin_api directory
    RewriteRule ^admin_api/(.*)$ index.php/$1 [L]

    # Handle remaining requests
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Explanation:

  • The first rule matches URLs starting with admin_api/ and rewrites them to index.php with the remaining part of the URL appended.
  • The second rule remains the same and handles other non-existing files or directories.

2. Verify CodeIgniter Configuration:

  • Ensure the base URL in your CodeIgniter config.php file is still set to http://admin.app-dev/ .

3. Restart Apache:

After making changes to .htaccess , restart Apache for the new configuration to take effect.

Additional Considerations:

  • If you have custom routes defined in routes.php , ensure they handle requests starting with admin_api/ appropriately.

With these changes, your URL rewriting should work as expected, and requests like http://admin.app-dev/admin_api/login should be routed to the appropriate controller and method within your CodeIgniter application.