October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

Remove index.php from the Address Bar in Apache

Serving index.php as Apache's directory index does not remove it from explicit URLs. Learn the safe, context-aware redirect pattern and how to test it without loops.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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 by DirectoryIndex and normally serves the matching file internally.
  • https://www.example.com/index.php explicitly names a file. Serving index.php for 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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

.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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Test before making the redirect permanent

  1. Open the clean root URL and confirm the page loads without changing the address to /index.php.
  2. Request /index.php and confirm one external redirect ends at the intended scheme, host, and slash form.
  3. Repeat with a query string, such as /index.php?test=1, and verify whether the parameter should be retained.
  4. Exercise ordinary extensionless PHP URLs and representative subdirectory paths.
  5. Inspect response status and Location headers with browser developer tools or an HTTP client. A working canonicalization normally shows a single 301/302 followed by a 200 response.
  6. 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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 (http or https) and hostname (www or 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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.