Skip to main content
Home
GrN.dk

Main navigation

  • Articles
  • Cases
  • Services
  • Your Digital Project Manager
  • About Greg Nowak
  • Image Gallery
  • Contact
User account menu
  • Log in

Join my community / free newsletter — sign up here

Breadcrumb

  1. Home

Canonical Apache Redirects: Remove index.html and index.php Cleanly

Illustrated infographic summarizing: Canonical Apache Redirects: Remove index.html and index.php Cleanly

By Greg Nowak. Updated 6 August 2026.

When /, /index.html, and /index.php display the same page, the website has several public addresses for one piece of content. Visitors may not notice, but duplicate URLs fragment analytics, make campaign reports harder to interpret, and add unnecessary checks to migrations and agency handovers.

The practical fix is to choose one public URL and permanently redirect the alternatives to it. For most businesses, that means HTTPS, one preferred hostname, and no visible default filename. The important part is not finding a clever rewrite snippet. It is making the policy explicit, avoiding redirect chains, and ensuring the rest of the website uses the same URLs.

Define the final URL before touching Apache

Decide whether the canonical hostname is example.com or www.example.com. Then decide whether directory URLs should end with a slash. For a request such as /services/index.php, the usual destination is https://example.com/services/.

Treat this as a small URL-governance decision rather than an isolated SEO fix. Internal links, canonical elements, XML sitemaps, hreflang annotations, campaign templates, and reporting dashboards should all point to the chosen version. Google regards permanent server-side redirects as a strong canonicalization signal, but consistent supporting signals make the result easier to understand and maintain.

Environment Recommended approach Main risk to check
Shared hosting Use a document-root .htaccess rule Existing CMS rules or hosting-panel redirects
Managed Apache server Prefer virtual-host configuration Splitting related redirects across different scopes
Reverse proxy or load balancer Use protocol information supplied by a trusted proxy Loops caused by Apache seeing every request as HTTP
Active migration Build and test a complete old-to-new URL map Redirecting everything to the home page or adding extra hops
Choose the redirect location according to who controls the infrastructure and where HTTPS terminates.

A practical document-root .htaccess rule

The following Apache 2.4 example removes explicitly requested index.html and index.php filenames, while also enforcing HTTPS and a non-www hostname. Replace example.com with the real canonical domain. These patterns assume the file is the document-root .htaccess; patterns differ in virtual-host context.

RewriteEngine On

# Explicit index file: go directly to the final host and protocol
RewriteCond %{THE_REQUEST} "\s/+(.*/)?index\.(?:html|php)(?:[?\s])" [NC]
RewriteRule ^(.*/)?index\.(?:html|php)$ https://example.com/$1 [R=301,END]

# Every other request: enforce HTTPS and the canonical hostname
RewriteCond %{HTTP_HOST} !^example\.com$ [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,END]

Rule order matters. A request for http://www.example.com/services/index.php should go straight to https://example.com/services/, rather than changing protocol, hostname, and filename in three separate responses.

The fixed destination hostname also avoids reflecting an arbitrary incoming Host header into the redirect. If the site legitimately serves several domains, write an explicit policy for each one instead of turning unvalidated request data into a destination URL.

Why THE_REQUEST prevents CMS redirect loops

THE_REQUEST contains the original HTTP request line sent by the client. It therefore distinguishes a visitor asking for /index.php from a CMS internally routing a clean URL through index.php. Redirecting based only on an internally rewritten path can interfere with front-controller applications or create loops.

The rule does not alter Apache’s DirectoryIndex behavior. Apache can continue serving index.php internally when someone visits /; the filename simply disappears from the public address.

Query strings are retained because the redirect target does not define a new one. Thus, /services/index.php?utm_source=newsletter becomes /services/?utm_source=newsletter. If a legacy parameter must be removed deliberately, use Apache’s QSD flag and document the reason. Do not discard tracking or application parameters accidentally.

Handle server configuration and proxies deliberately

When you control Apache, virtual-host configuration is usually easier to audit and more efficient than distributed .htaccess files. Apache recommends the simpler Redirect directive when a plain prefix redirect is sufficient. However, mixing a protocol redirect in one scope with filename-removal rules in another can introduce an extra hop. Test the combined cases, not just each rule separately.

If TLS terminates at a reverse proxy, %{HTTPS} may be off on the backend even though the browser used HTTPS. In that architecture, use the proxy’s forwarded-protocol value only when the proxy overwrites the header, direct backend access is restricted, and the trust boundary is documented. Otherwise, a spoofed header can bypass policy or an incorrect condition can create an endless redirect.

Also verify certificate renewal after changing port 80 behavior. Let’s Encrypt recommends keeping port 80 available and supports HTTP-to-HTTPS redirects for HTTP-01 validation, but the complete proxy, firewall, and ACME-client path still needs a real renewal test.

Test before making the redirect permanent

Use a temporary 302 while validating a new rule if browser caching could slow down corrections. Once the map is correct and intended to remain, change it to 301.

curl -IL 'http://www.example.com/index.php'
curl -IL 'https://www.example.com/services/index.html?utm_source=test'
curl -IL 'https://example.com/services/'

For each case, confirm that the first response points directly to the intended URL, the final response is 200, the query string survives where required, and the clean URL does not redirect again. Then crawl the site for old internal links and update sitemaps, canonical elements, campaign templates, monitoring checks, and documentation.

A redirect rule is small infrastructure, but its effects reach search, analytics, advertising, and future development work. If your rules span a CMS, proxy, CDN, or migration map, Greg can help turn them into a testable implementation plan before they reach production.

Related on GrN.dk

  • Google’s AI Search toggle needs a test plan, not a gut decision
  • If the Facts Need JavaScript, AI Search May Miss the Full Page
  • Google’s 2026 AI Search Guidance: SEO Still Counts, Reporting Changes

Need help with this kind of work?

Talk to Greg about your redirect plan Get in touch with Greg.

Sources

  • Apache mod_rewrite Introduction
  • Apache RewriteRule Flags
  • Apache mod_rewrite Technical Details
  • Redirects and Google Search
  • Best Practice — Keep Port 80 Open
Last modified
2026-08-06

Tags

  • apache
  • mod_rewrite
  • Technical SEO
  • Canonical URLs
  • Redirects
  • Log in to post comments

Review Greg on Google

Greg Nowak Google Reviews

 

Written recommendations from Trafik og Veje, Aarhus Municipality (2011) and AgroTech (2010) — read them on LinkedIn.

Illustrated infographic summarizing: From Supplier PDFs to Product Data: Where AI Needs a Second Check
From Supplier PDFs to Product Data: Where AI Needs a Second Check
2026-09-07

Supplier files need more than extraction. Here’s how to check coverage, match SKUs, resolve unclear units and prices, and test product data before a catalogue import.

Illustrated infographic summarizing: Shorter TLS Certificates: Will Your Renewal Setup Keep Up?
Shorter TLS Certificates: Will Your Renewal Setup Keep Up?
2026-09-06

Shorter TLS certificates leave less room for renewal problems. Check domain validation, scheduling, deployment and the certificate your customers actually receive.

Illustrated infographic summarizing: Your AI Image Has Content Credentials. Will Your Website Keep Them?
Your AI Image Has Content Credentials. Will Your Website Keep Them?
2026-09-05

AI image credentials can disappear during routine website processing. Learn how to test your CMS, optimizer, CDN, and publishing workflow end to end.

Illustrated infographic summarizing: What Are Customers Asking? Let AI Find the Patterns in Support Tickets
What Are Customers Asking? Let AI Find the Patterns in Support Tickets
2026-09-04

AI-based ticket analysis can uncover recurring complaints, product defects and gaps in documentation—without the company needing yet another chatbot.

Illustrated infographic summarizing: OpenAI Has Machine Identity Now. Which Jobs Should Lose API Keys?
OpenAI Has Machine Identity Now. Which Jobs Should Lose API Keys?
2026-09-03

OpenAI’s X.509 workload identity can replace API keys for the right workloads. This practical framework helps teams decide where to start safely.

Illustrated infographic summarizing: WordPress 7.1 Exposes AI-Ready Actions. Who Gets to Run Them?
WordPress 7.1 Exposes AI-Ready Actions. Who Gets to Run Them?
2026-09-02

WordPress 7.1 helps AI agents discover and invoke site abilities. Here is how to keep exposure, authentication and permission firmly separate.

Illustrated infographic summarizing: From Sales Meeting to CRM: Automate Follow-Up Without Compromising Data Quality
From Sales Meeting to CRM: Automate Follow-Up Without Compromising Data Quality
2026-09-01

How to use AI for meeting notes and follow-up while fixed rules protect CRM data, customer matching and the sales pipeline from errors and premature changes.

Illustrated infographic summarizing: Your AI Gateway Can Name the User. Decide What That Log Is For
Your AI Gateway Can Name the User. Decide What That Log Is For
2026-08-31

Identity-aware AI Gateway logs can sharpen security and cost control, but only when attribution, access, retention, guardrails, and response are clearly defined.

Illustrated infographic summarizing: Zero Data Retention Is a Workflow Audit, Not a Checkbox
Zero Data Retention Is a Workflow Audit, Not a Checkbox
2026-08-30

Zero Data Retention covers the provider, not every copy in your stack. See how to audit endpoints, logs, storage, deletion and project-level controls.

Illustrated infographic summarizing: MCP 2026-07-28 Is an Auth Migration, Not a Version Bump
MCP 2026-07-28 Is an Auth Migration, Not a Version Bump
2026-08-29

MCP’s July 2026 release removes protocol sessions and tightens OAuth. Here’s a practical plan for migrating clients, servers and enterprise access safely.

More articles

Built by AI — available for your business. The daily articles on this site are researched, written and illustrated by an autonomous AI pipeline. At nowa.dk I install the same kind of AI automation in businesses at fixed prices — site in Danish, English version here, and web/marketing agencies have a dedicated page.

RSS feed

Footer

  • All articles
  • Contact

GrN.dk — AI automation, web platforms, web optimization, data handling and logistics.

© 2026 GrN.dk · LinkedIn · Contact · AI automation in Danish: nowa.dk

Behind GrN.dk: Individual Entrepreneur Codecrafter · Tax ID 305669096 · Bakhtrioni St. 22, 0194 Tbilisi, Georgia · official business register