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

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

By Greg Nowak. Updated 13 October 2026.

If /, /index.html, and /index.php display the same page, your website has several addresses for one destination. Those variants can spread into campaign links, agency reports, and navigation. Cleaning them up gives your team one address to publish and maintain.

The usual solution is a permanent Apache redirect to the directory URL, keeping the visitor’s intended page and query parameters. The business brief is straightforward: old links should work, campaign tracking should survive, and enquiries should still arrive. The implementation needs to account for your CMS, hosting, and any proxy in front of Apache.

Agree the destination before writing rules

Compare the content and behaviour at each address first. An old index.html landing page and a newer index.php application are not automatically equivalent. If they differ, decide which content to retain before redirecting either one.

Preserve the directory: /services/index.php should normally become /services/, rather than the homepage. Choose the preferred hostname and HTTPS destination at the same time, so visitors can reach the final address in one redirect.

Google recommends permanent server-side redirects for permanent URL changes and treats them as a canonicalization signal. That supports a consistent URL preference; it does not guarantee a ranking increase.

Choose where the redirect belongs

Website setup Suitable approach Decision before implementation
Shared hosting Document-root .htaccess rules Confirm rewrite permissions and CMS rule placement.
Apache server you control Main configuration or virtual-host rules Adapt the patterns to that configuration context.
CDN or reverse proxy Coordinate with the public HTTPS layer Identify which layer controls protocol and hostname redirects.
Mixed legacy content Explicit URL mappings Approve the content destination for each exception.
Assign one owner to check the complete redirect journey across hosting, application, and marketing links.

Apache recommends the main configuration when you have access. On shared hosting, confirm that mod_rewrite is enabled and rewrite directives are permitted through AllowOverride FileInfo or an appropriate AllowOverrideList. An ignored or disallowed file needs a hosting configuration fix.

A document-root .htaccess example

This Apache 2.4 example handles GET and HEAD requests, combines filename removal with hostname and HTTPS normalization, and assumes Apache receives the public TLS connection. Replace example.com with your preferred domain. Place it before CMS routing rules, outside any block the CMS regenerates.

RewriteEngine On

# Explicit index request: redirect to the final directory URL
RewriteCond %{REQUEST_METHOD} ^(?:GET|HEAD)$
RewriteCond %{THE_REQUEST} "\s/+(?:[^?\s]*/)?index\.(?:html|php)[?\s]"
RewriteRule ^(.*/)?index\.(?:html|php)$ https://example.com/$1 [R=301,END]

# Other page requests: normalize hostname and protocol
RewriteCond %{REQUEST_METHOD} ^(?:GET|HEAD)$
RewriteCond %{HTTP_HOST} !^example\.com$ [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,END]

For example, http://www.example.com/services/index.php should redirect directly to https://example.com/services/. The destination hostname is fixed rather than copied from the request.

These patterns are for a document-root .htaccess and ordinary, unencoded paths. Moving them into a virtual host or subdirectory requires adjustment. Review uppercase filenames, encoded characters, and routes such as /index.php/products separately. HTTPS aliases also need valid certificates before a redirect can happen.

Protect CMS routing and campaign parameters

THE_REQUEST checks the original client request. It distinguishes someone requesting /index.php from a CMS internally routing a clean URL through that file. Without that distinction, an index-removal rule can interfere with normal routing. See Apache’s mod_rewrite reference for request variables and rule contexts.

Neither destination in this example supplies a query string, so Apache retains the existing one. A request for /services/index.php?utm_source=newsletter should become /services/?utm_source=newsletter. You do not need QSA here. The QSD flag discards the entire query string; use it only after checking that no tracking or application parameters are needed. These behaviours are documented in Apache’s rewrite flags guidance.

The method conditions deliberately leave POST submissions outside this example. If forms, payment callbacks, or integrations use an index URL, agree their routing separately and retain the existing HTTPS policy for those endpoints.

Check that DirectoryIndex serves the intended content at the clean directory URL. Keep DirectoryIndexRedirect off for this approach: enabling it redirects back to the index resource and can create a loop. Leave normal directory slash handling enabled unless your application has a specific requirement. See Apache’s mod_dir documentation.

Check the hosting stack for competing redirects

If a proxy terminates HTTPS, Apache may see HTTP even when the visitor used HTTPS. The example’s HTTPS condition then needs an infrastructure-specific replacement. Trust forwarded protocol information only from a known proxy that overwrites the header, with direct backend access restricted.

Review hosting-panel redirects, CDN rules, and CMS plugins together. An earlier HTTP-to-HTTPS rule may add a hop before filename removal. This example also does not combine every trailing-slash correction. Test through the public hostname, and check certificate-renewal routes if you change HTTP handling.

Test the visitor journey before making it permanent

Save the previous configuration and test in staging. Use R=302 in both rules during initial checks, then switch to R=301 when the mapping is approved.

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

For these examples, expect one redirect from each old address and a final 200 response. The preferred clean URL should return directly. Inspect every Location header and confirm that the directory and query string survive. These commands use HEAD; also test browser GET requests, page content, and an actual enquiry submission.

Finish by updating internal links, canonical elements, sitemaps, and campaign templates. Record the mappings, exceptions, test results, and rollback steps. After release, check logs for unexpected redirects or errors.

If the work spans your host, developer, and marketing agency, Greg can help coordinate the redirect plan and rollout checks. Bring a few affected URLs and the names of the systems involved.

Regex redirects and rewrite references

Reading on redirects with regular expressions:

RewriteRule generator: https://donatstudios.com/RewriteRule_Generator

Related on GrN.dk

Need help with this kind of work?

Plan your redirect rollout with Greg Get in touch with Greg.

Sources

Seneste artikler

Når kunden beder om indsigt, kan AI hjælpe med at samle materiale fra WordPress og Microsoft 365. Se, hvordan I holder styr på datakilder, ansvar og kontrol.

Når en ingrediens ændres, kan menukortet kræve rettelser, selv om opskriftsdatoen står stille. Sådan kan AI hjælpe med gennemgang og godkendelse i WordPress.

Når checkout fejler, skal driftspartneren have noget konkret at arbejde med. Se, hvordan AI, dmesg og journalctl kan samle sporene i en brugbar driftssag.

Brug oktober til at afprøve daglige AI-forslag til genbestilling før Black Friday. Få styr på Shopify-data, leveringstid og budget, før forslagene bliver til indkøb.

Jeg lærte serverdrift ved at ødelægge mine egne servere. Jeg søger en, der vil stå ved siden af mig, mens jeg gør det, og så gøre det selv ugen efter.

Jeg er god til at bygge og dårlig til at ringe. Her er, hvem jeg vil have ved siden af mig, hvad der er lettest at sælge, og hvordan vi deler det.

AI kan samle onboardingopgaverne før første arbejdsdag. Se, hvordan lederen godkender konkret adgang, og hvordan åbne opgaver bliver fulgt til dørs.

En AI-assistent kan svare på spørgsmål og føre kunder til booking. Her er de konkrete grænser for pris, levering, personoplysninger og kontakt med en medarbejder.

Et sikkert AI-workflow kan omsætte Meet- og Teams-transskripter til godkendte beslutninger og opgaver i Jira eller Asana – uden at slippe kontrollen.

AI kan finde opsigelsesfrister og prisreguleringer i leverandørkontrakter, sende usikre fund til godkendelse og oprette de rette påmindelser.