A clean CodeIgniter 4 deployment on Apache or Hostinger frequently passes homepage verification at https://example.com/—and then fails immediately on every inner route with 404 Not Found, awkward /public/index.php/ path leaks in redirects, or infinite trailing-slash loops. Every one of those symptoms traces back to how Apache's mod_rewrite hands requests into CodeIgniter's separated public/ front controller and how app/Config/App.php reconstructs canonical URLs.
Failure Symptoms & Root Causes at a Glance
| Observed Behavior | Exact Root Cause | Verified Production Fix |
|---|---|---|
/ returns 200, while /tools or /about returns Apache 404 |
public/.htaccess missing, disabled (`AllowOverride None`), or host webroot sits at project root without forwarding to public/ |
Enable mod_rewrite (`AllowOverride All`) and use dual root + public/.htaccess |
/tools/ redirects (`301`) to /public/tools |
public/.htaccess strips trailing slashes using relative %1 after root rewritten public/ into REQUEST_URI |
Strip trailing slashes in root `.htaccess` before forwarding to `public/`, and set explicit `RewriteBase /` |
redirect() links contain /index.php/ |
app/Config/App.php still has default $indexPage = 'index.php' and `.env` did not override it |
Set public string $indexPage = ''; in App.php and app.indexPage = '' in .env |
| Works on Windows XAMPP, returns 404 on Linux Hostinger | Windows/macOS filesystems are case-insensitive; Linux treats Home.php vs home.php and view paths as strictly distinct |
Match controller filenames, PSR-4 namespaces, and view('folder/file') lowercase names identically |
Legacy 301 filter URLs (e.g. /blog, /feed) return 404 instead of 301 |
Redirect filter registered in Filters::$globals['before'] instead of $required['before'] |
Place redirect filter inside $required['before'] so it runs before CI4 throws PageNotFoundException |
1. Root .htaccess vs public/.htaccess on Shared Hosting
On VPS servers where you control Nginx or Apache VirtualHost configs directly, pointing DocumentRoot straight at /var/www/site/public is cleanest because sensitive files in app/, writable/, and .env stay outside the webroot completely. On Hostinger shared/cloud panels, however, public_html/ itself is often the fixed domain root containing both root project folders and public/.
When both live inside public_html, place two carefully ordered `.htaccess` files so external URLs never reveal /public/ and sensitive directories stay blocked (`403 Forbidden`):
# File 1: public_html/.htaccess (Project Root)
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
# 1. Strip www. and enforce HTTPS cleanly (works for live domain & staging subdomains)
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# 2. External /public/ guard: inspect %{THE_REQUEST} (original client line), NOT internal rewrites!
RewriteCond %{THE_REQUEST} \s/+public(?:/([^\s?]*))?[\s?] [NC]
RewriteRule ^ https://%{HTTP_HOST}/%1 [R=301,L]
# 3. Strip trailing slash BEFORE internal rewrite to public/
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ https://%{HTTP_HOST}/$1 [R=301,L]
# 4. Block direct HTTP access to framework folders and dotfiles
RewriteRule ^(?:app|system|writable|tests|vendor)(?:/|$) - [F,L]
RewriteRule ^(?:\.env|composer\.(?:json|lock)|spark|phpunit\.xml.*)$ - [F,L]
# 5. Serve real webroot static files directly (e.g., ads.txt, legacy /wp-content/)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# 6. Hand clean routes internally to CodeIgniter's public/ front controller
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Checking %{THE_REQUEST} in Rule 2 is critical: once Rule 6 internally appends public/, checking %{REQUEST_URI} instead of %{THE_REQUEST} would bounce visitors into an infinite `301` redirect loop!
# File 2: public_html/public/.htaccess (Inside public/)
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Front controller fallback for clean URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
2. Configure app/Config/App.php and .env Together
Even with perfect Apache rewrites, CodeIgniter generates wrong links or fails CSRF/canonical checks if App.php keeps starter placeholders like http://localhost:8080/ or $indexPage = 'index.php':
<?php
// app/Config/App.php
public string $baseURL = 'https://technolila.com/';
public string $indexPage = '';
public string $uriProtocol = 'REQUEST_URI';
# .env (staging or production override)
CI_ENVIRONMENT = production
app.baseURL = 'https://technolila.com/'
app.indexPage = ''
3. Understand CI4 Filter Order: $required['before'] vs $globals['before']
A subtle CI4 routing trap hits nearly every site maintaining legacy 301 or 410 redirect maps: in CodeIgniter 4, `$filters->globals['before']` only execute after `Routes.php` successfully matches a controller route! If a visitor requests an old retired WordPress URL like /blog or /webtool/tools/cost-comparison that has no entry in `Routes.php`, CI4 throws `PageNotFoundException` (`404`) before `$globals['before']` runs.
Register any legacy redirect filter inside $required['before'] in app/Config/Filters.php so it intercepts and issues clean `301`/`410` responses before router matching runs:
<?php
// app/Config/Filters.php
public array $required = [
'before' => [
'forcehttps',
'legacyRedirect', // Runs before tryToRouteIt() so legacy non-routed paths 301 cleanly
'pagecache',
],
// ...
];
4. Debug Routes & Case Sensitivity on Linux Servers
- Inspect Compiled Route Precedence: Run
php spark routesvia CLI to verify catch-all patterns (such as(:segment).html) do not sit above more specific static routes or mask valid controllers. - Keep
$routes->setAutoRoute(false);Disabled in Production: Explicit routing prevents arbitrary controller method URLs and improves predictable error reporting. - Verify Linux Case Exactness: Confirm controller class names (`Tools.php`), view directories (`app/Views/tools/show.php`), and route targets match exact uppercase/lowercase spelling cleanly.
- Test One Redirect Hop With
curl: Runcurl -sI https://technolila.com/tools/json-formatter/and verify a single `301` directly tohttps://technolila.com/tools/json-formatterwithout/public/or `http://` hops.
Explore our CodeIgniter 4 CRUD Tutorial and CI4 Database Troubleshooting Guide for complete production hardening.
5. Flush Stale Page & Edge Caches After Fixing Rewrite Rules
After updating root .htaccess or Routes.php on shared hosting, stale edge or browser `301` caches can keep replaying old redirects. Clear CodeIgniter's writable page cache (`writable/cache/`), purge CDN cache in your hosting dashboard, and retest with curl -sI -H "Cache-Control: no-cache" against both trailing-slash and clean routes.
Frequently Asked Questions
Why does Apache still return 404 after uploading .htaccess on Ubuntu/Debian VPS?
Apache ships with mod_rewrite disabled and AllowOverride None by default on many Linux distributions. Enable rewrite via sudo a2enmod rewrite, set AllowOverride All inside your site's <Directory> block, and reload Apache.
Is moving public/index.php directly into public_html/ recommended?
Not unless you relocate app/, writable/, vendor/, and .env one directory above public_html and update Paths.php cleanly—keeping CI4's folder separation intact is much safer on shared hosts.
Can index.php/ in URLs hurt SEO?
Serving both /tools and /index.php/tools creates duplicate crawlable URLs unless canonical tags and `301` redirects consolidate cleanly onto root-clean URLs.