Why index.php Works But / Doesn't on Apache – And How to Fix It with .htaccess

Why index.php Works But / Doesn't on Apache – And How to Fix It with .htaccess
Photo by jose aljovin / Unsplash

If you're setting up a PHP application on an Apache web server, you may run into a strange issue:
Visiting https://example.com/myapp/index.php works perfectly, but when you try https://example.com/myapp/, it either shows a 403 Forbidden, 404 Not Found, or directory listing, depending on your setup.

So what's going on? And how do you fix it?


🧩 The Problem: Apache Needs Direction

Apache does not automatically know that / should resolve to index.php unless:

  • DirectoryIndex is set correctly, or
  • mod_rewrite is enabled and .htaccess is configured to redirect requests.

By default, Apache will look for an index.html, index.php, or whatever is defined in its DirectoryIndex directive. But when you're using routing in your PHP app (e.g., Laravel, CodeIgniter, or a custom router), it’s not just about serving the index file — it’s about redirecting all requests to that file so your router can handle it.


🔧 The Solution: Use .htaccess with mod_rewrite

To make https://example.com/myapp/ load the same as index.php, you’ll need a .htaccess file in your /myapp directory that uses mod_rewrite to route all requests to index.php.

Here’s the recommended .htaccess content:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /myapp/

    # Serve existing files and directories as-is
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # Otherwise, redirect to index.php
    RewriteRule ^ index.php [L]
</IfModule>

📌 Explanation:

  • RewriteEngine On: Enables the rewrite engine.
  • RewriteBase /myapp/: Tells Apache where this app is located.
  • RewriteCond lines: If the request is not an actual file or folder…
  • RewriteRule ^ index.php: …rewrite it to index.php.

🔍 Checklist: Things You Must Check

✅ 1. mod_rewrite is enabled

Make sure Apache has the rewrite module enabled:

sudo a2enmod rewrite
sudo systemctl restart apache2

✅ 2. Apache is configured to allow .htaccess overrides

In your Apache config (e.g., /etc/apache2/sites-available/000-default.conf), make sure the directory block includes:

<Directory /var/www/html/myapp>
    AllowOverride All
    Require all granted
</Directory>

Restart Apache after editing:

sudo systemctl restart apache2

✅ 3. File permissions and ownership

Make sure Apache can read your .htaccess and index.php files:

sudo chown -R www-data:www-data /var/www/html/myapp
sudo chmod -R 755 /var/www/html/myapp

⚠️ Common Mistakes to Avoid

  • Missing .htaccess in the target folder (/myapp).
  • .htaccess file exists but mod_rewrite is disabled.
  • Wrong RewriteBase (if you copy it from a different setup and forget to update).
  • Apache is set to AllowOverride None — which disables .htaccess usage altogether.

🧠 Bonus: Use DirectoryIndex as Fallback

You can also add a fallback in your .htaccess:

DirectoryIndex index.php

But again — this helps only when you're not relying on routing and just want to load the file. For frameworks or apps with dynamic routes (e.g., /user/profile/3), the rewrite rule is essential.


🚀 Finally

To ensure that visiting /myapp/ loads your app just like index.php, you must configure Apache to rewrite requests using .htaccess and mod_rewrite. It’s not just a convenience — it’s essential for routing in modern PHP applications.

Once configured properly, your users can visit cleaner URLs, and your app can handle routing logic seamlessly through index.php.

Support Us