How does Joomla remove index.php from URLs on Apache?

The Citead team, updated

In short. Joomla uses Apache mod_rewrite and a root .htaccess file renamed from htaccess.txt to route clean URLs through index.php.

Definition: Joomla removes index.php from a public SEF URL on Apache by combining the Search Engine Friendly URLs and Use URL Rewriting settings with a root .htaccess file renamed from htaccess.txt. Apache then internally rewrites the clean route to Joomla's index.php script.

Key facts

  • Joomla defines a route as the local part of a SEF URL, after the domain name, and describes routing as the process that creates and processes those URLs. The Search Engine Friendly URLs guide uses that terminology.
  • In Global Configuration, Joomla's guide tells administrators to set Search Engine Friendly URLs to Yes and Use URL Rewriting to Yes. Add Suffix to URL is an optional setting.
  • For an Apache server, the same guide instructs administrators to rename htaccess.txt in the site root to .htaccess.
  • Joomla's Apache SEF guide states that the Use Apache mod_rewrite/URL rewriting option uses Apache's mod_rewrite function to eliminate the index.php portion of the URL.
  • The bundled file is also documented as containing instructions that implement SEF URLs. Joomla's htaccess.txt documentation places it in the Joomla root directory.

What changes when URL rewriting is enabled?

Joomla's Search Engine Friendly URLs guide shows the route becoming cleaner in stages. With SEF URLs enabled but URL rewriting disabled, the path still contains index.php. When SEF URLs, URL rewriting, and .htaccess are enabled, the example uses a path without index.php and with the optional .html suffix enabled.

https://www.example.com/index.php/sample-layouts/articles
https://www.example.com/sample-layouts/articles.html

The first address is the guide's example for SEF URLs without rewriting. The second is its example after URL rewriting and .htaccess are enabled, with Add Suffix to URL set to Yes. The suffix is a separate option from the rewrite that removes index.php.

What does the renamed .htaccess file do?

Apache uses .htaccess for site-specific configuration. Joomla's htaccess.txt documentation says that Joomla supplies htaccess.txt in the root directory and describes renaming that file to .htaccess together with the SEO settings. The same page says that Apache processes an additional parent-directory .htaccess first when Joomla is installed in a subdirectory.

Apache's active per-directory filename defaults to .htaccess, represented in the server configuration as:

AccessFileName .htaccess

The Joomla SEF section is guarded by mod_rewrite. For a public frontend request that is not already /index.php and does not match a physical file or directory, the supplied rule internally rewrites the request to index.php:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/index\.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* index.php [L]
</IfModule>

This internal rewrite routes the request to Joomla's index.php script while the address shown in the browser remains the SEF route.

How is Apache configured for the rewrite?

Joomla's Apache-specific instructions begin by checking that the server permits .htaccess overrides. They show AllowOverride All inside a <Directory> section in a virtual host or httpd.conf configuration. If overrides are disabled, Joomla's root .htaccess is ignored or causes an error.

<Directory "/home/user/public_html">
  AllowOverride All
</Directory>

The same instructions then put the file rename before enabling Use Apache mod_rewrite/URL rewriting. If Joomla is installed in its own folder, the guide says to uncomment RewriteBase / and add the Joomla folder name. If mod_rewrite cannot be enabled, the documented action is to leave the option off.

FAQ

Do you rename htaccess.txt before enabling URL rewriting?

Yes. Joomla's Apache instructions put renaming the file to .htaccess before enabling the URL rewriting option.

What should you check when .htaccess is ignored?

Check the Apache virtual host or httpd.conf configuration for an AllowOverride setting that permits the directives. Joomla's example uses AllowOverride All in the relevant <Directory> section.

Should you edit htaccess.txt directly?

Joomla's documentation says changes to htaccess.txt can be overwritten by a Joomla update. The active file is .htaccess, so update-specific changes belong there after checking the supplied file.

Sources

  1. Joomla Documentation, "Search Engine Friendly URLs", undated
  2. Joomla Documentation, "SEF URLs on Apache", undated
  3. Joomla Documentation, "The htaccess.txt file", undated

Check how a page reads to answer engines. Open the GEO score analyzer

Test a page and see what to fix first

The analysis usually takes less than 10 seconds and needs no account.

400,000+ pages scored in the public benchmark