Set the directory index and canonicalize explicit index requests separately. DirectoryIndex index.php makes / serve the PHP file, while a narrowly scoped mod_rewrite redirect changes a browser request for /index.php to /. Keeping those operations separate avoids redirect loops and protects other extensionless PHP routes.
What you are changing
Apache handles two different requests differently:
https://www.example.com/asks for a directory. Apache searches the resources listed byDirectoryIndexand normally serves the matching file internally.https://www.example.com/index.phpexplicitly names a file. Servingindex.phpfor the directory does not, by itself, redirect this URL.
The desired canonical result is therefore:
| Request | Expected result |
|---|---|
/ |
Page is served by index.php; the address remains /. |
/index.php |
External redirect to /. |
/about or another extensionless route |
Existing application rule continues to handle it. |
Configure the directory index
In the virtual-host configuration, or in an allowed .htaccess file, set:
DirectoryIndex index.php
Apache’s DirectoryIndex directive defines which resources it looks for when a client requests a directory. It is an internal selection step, not a canonical redirect.
Redirect only an explicit /index.php request
Use the request line recorded in THE_REQUEST to distinguish a browser request from an internal rewrite. In a document-root .htaccess, a narrowly scoped example is:
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallCrashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minute#1 Best Overall
RewriteEngine On
RewriteCond %{THE_REQUEST} s/+index.php(?:[?s]) [NC]
RewriteRule ^index.php$ / [R=301,L]
THE_REQUEST contains the complete HTTP request line sent by the client, such as GET /index.html HTTP/1.1. The condition therefore matches an explicitly requested filename, rather than every internal use of index.php. The rule pattern has no leading slash because Apache removes the directory prefix when matching rules in per-directory context.
Use a temporary redirect while testing:
RewriteRule ^index.php$ / [R=302,L]
After confirming the result, change 302 to 301 if the redirect is intended to be permanent. Browsers and search engines cache permanent redirects, so do not begin with a 301 while the rules are still changing.
Preserve query strings deliberately
If the request is /index.php?ref=mail, Apache normally carries the original query string when the redirect substitution does not define a new one. If your surrounding rules create or replace query strings, review the result explicitly. The [QSA] flag appends an original query string to one created by a rewrite; it is not a universal requirement for this redirect.
Choose the correct Apache context
Virtual-host or server configuration
Server and virtual-host configuration gives the administrator the broadest control and avoids many per-directory translation surprises. Put the rule in the appropriate <VirtualHost> context, test the configuration, and reload Apache according to your hosting platform.
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →.htaccess
Shared hosting commonly permits only .htaccess. Rewrite rules must be enabled by the host (typically through the relevant AllowOverride settings), and the rule pattern is relative to the directory containing the file. Existing host rules, aliases, canonical-host redirects, and extension-hiding rules can change the outcome.
Do not copy a historical multi-rule snippet as a universal solution. In particular, [R] performs an external redirect, while [PT] passes the resulting URI back through Apache’s URL-mapping translators such as an Alias or another redirect. They solve different problems and are only useful when the surrounding configuration requires them.
Rank #3
- Used Book in Good Condition
Prevent loops with existing extensionless PHP rules
Many sites already rewrite requests such as /contact to /contact.php. A broad rule that redirects any path ending in index.php, or a rule that runs again after the directory index is selected, can produce a loop, a 404, or a 500 error. Matching both the explicit request line and the exact root-level filename limits that risk.
If the application has index files in subdirectories, decide their canonical URLs separately. A root rule for ^index.php$ should not silently redirect /admin/index.php or another directory’s entry point.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Test before making the redirect permanent
- Open the clean root URL and confirm the page loads without changing the address to
/index.php. - Request
/index.phpand confirm one external redirect ends at the intended scheme, host, and slash form. - Repeat with a query string, such as
/index.php?test=1, and verify whether the parameter should be retained. - Exercise ordinary extensionless PHP URLs and representative subdirectory paths.
- Inspect response status and
Locationheaders with browser developer tools or an HTTP client. A working canonicalization normally shows a single 301/302 followed by a 200 response. - Only after these checks, replace the temporary redirect with a permanent one and clear any cached test redirects if necessary.
Common failure symptoms
Redirect loop
The redirect is probably matching an internally rewritten request or another canonical redirect is sending the request back. Tighten the THE_REQUEST condition, inspect all rewrite and host-normalization rules, and test one redirect at a time.
404 or 500 response
Check the rule’s context, escaping, and permissions. In .htaccess, remove the leading slash from the RewriteRule pattern. Also look for a malformed character—one SitePoint poster reported that an erroneous backslash caused failures until it was removed.
Other extensionless pages stop working
The new rule may be too broad or may be ordered ahead of the site’s existing PHP rewrite. Restrict it to the root-level index.php request and preserve the established route rules.
The URL still shows /index.php
Confirm that mod_rewrite is enabled, that the rule is loaded in the active virtual host or directory, and that another redirect is not taking precedence. A DirectoryIndex setting alone will not change an already explicit URL.
Best Value
What the historical SitePoint example proves—and does not
A November 28, 2010 SitePoint discussion describes the same goal after PHP extensions had been hidden. The original poster reported success locally and on the hosting account after correcting a stray backslash, but the thread also records loops and 404/500 errors during earlier iterations. That is a host-specific case report, not an independently reproduced configuration or a guarantee that its complete snippet fits another Apache installation.
The thread mentions Options -MultiViews, but it does not establish that the directive is generally required or that it caused the final result. Enable or change it only when your host’s documented configuration and your site’s routing require it.
Canonical URL decisions to make explicitly
- Choose the canonical scheme (
httporhttps) and hostname (wwwor non-www) rather than letting separate rules compete. - Choose whether the directory URL ends with a slash. Apache directory handling generally expects the slash, while an application-level route may choose another policy.
- Apply the policy consistently to links, redirects, cookies, and any web-application URL generation.
The Bottom Line
Use DirectoryIndex index.php to serve the home page at /, then add a narrowly matched THE_REQUEST-based external redirect for explicit /index.php requests. Test it alongside every existing rewrite before adopting a permanent redirect.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Free tools Windows power users keep installed
One-click scans. No signup required.




