Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124

When we manage a Joomla site we quickly discover that the .htaccess file is one of the most powerful tools at our disposal. It lives in the Joomla root directory and lets us instruct the Apache web server on how to handle requests, protect sensitive resources, and speed up content delivery. By tweaking a few directives we can block common attacks, enforce HTTPS, enable GZIP compression, and fine‑tune browser caching—all without touching Joomla’s core code.
In this guide we walk through every step needed to get the joomla htaccess file up and running, then we go through the security and performance tweaks that keep our sites safe and fast. We cover the default htaccess.txt that ships with Joomla, the changes introduced in Joomla 5, and the most common pitfalls you may encounter. All code snippets are ready to copy‑paste into your own .htaccess file.
Apache reads .htaccess files on a per‑directory basis. When a request arrives, Apache walks the directory tree from the document root to the requested file, merging directives from each .htaccess it encounters. This means that a rule placed in the Joomla root can affect every subfolder, while a rule in images/ only applies to that folder.
Because .htaccess is processed on every request, it is ideal for lightweight tasks such as URL rewriting, header injection, and access control. However, heavy processing can add latency, so we keep the file concise and avoid unnecessary modules.
Joomla ships a htaccess.txt file that contains a solid baseline, as described in the official Joomla htaccess documentation. When we rename it to .htaccess, Apache begins to obey its directives. The default file includes:
mod_rewrite (Apache mod_rewrite documentation) for SEF (search engine friendly) URLs — see our Joomla SEF URLs configuration guide for the full setup.www to non‑www (or vice‑versa) based on configuration.We will build on this foundation, adding our own security headers and performance tweaks.
Most of us use an FTP client or the hosting control panel’s file manager. The steps are simple:
public_html or www).htaccess.txt..htaccess (note the leading dot).644 permissions).If we have SSH access we can rename the file with a single command:
mv htaccess.txt .htaccess
chmod 644 .htaccess
These commands work on most Linux hosts. After renaming, we should clear any server‑side caches to make sure Apache picks up the new file.
A quick test is to add a harmless rule that returns a 403 status for a test URL:
# Test rule – deny access to /test-deny
Redirect 403 /test-deny
Then browse to https://yourdomain.com/test-deny. If we see a “Forbidden” page, Apache is processing the file correctly. Remember to remove the test rule afterward.
Attackers often try to inject malicious payloads via query strings. We can stop many of these attempts with a few RewriteCond checks:
# Block base64 injection attempts
RewriteCond %{QUERY_STRING} (^|&)base64_decode\(.*\) [NC,OR]
RewriteCond %{QUERY_STRING} (^|&)eval\(.*\) [NC,OR]
RewriteCond %{QUERY_STRING} (^|&)GLOBALS\(.*\) [NC]
RewriteRule ^ - [F]
# Block request for known vulnerable files
RewriteCond %{REQUEST_URI} (wp-config\.php|\.env|\.git) [NC]
RewriteRule ^ - [F]
These rules return a 403 (Forbidden) response before Joomla even loads, reducing the attack surface. For a broader list of hardening measures, check our Joomla security tips guide.
Even though Joomla’s default file disables directory listings, we reinforce it with an explicit Options -Indexes directive:
# Disable directory listing
Options -Indexes
Modern browsers respect security headers that mitigate XSS, click‑jacking, and data injection. We add them via mod_headers:
# Enable mod_headers
# Content Security Policy – adjust sources as needed
Header set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:;"
# Prevent click‑jacking
Header always set X-Frame-Options "SAMEORIGIN"
# XSS protection (deprecated in newer browsers but still useful)
Header set X-XSS-Protection "1; mode=block"
# MIME sniffing protection
Header set X-Content-Type-Options "nosniff"
# Referrer policy
Header set Referrer-Policy "strict-origin-when-cross-origin"
We can tailor the CSP (CSP reference guide) to allow third‑party scripts or fonts if our site uses them.
Joomla’s configuration file and log files must never be served to the public. We explicitly deny access:
# Protect configuration.php
Order allow,deny
Deny from all
# Protect .htaccess itself
Order allow,deny
Deny from all
>
# Protect log files
Order allow,deny
Deny from all
If we have a static IP range for our office, we can lock the Joomla administrator folder to that range:
# Allow only trusted IPs to /administrator
Order deny,allow
Deny from all
Allow from 203.0.113.0/24
Replace /path/to/joomla with the actual server path. For shared hosts that don’t allow <Directory> blocks, we can use RewriteCond %{REMOTE_ADDR} instead.
Compressing HTML, CSS, and JavaScript reduces bandwidth and speeds up page loads. We cover additional speed strategies in our Joomla speed optimization guide. The following directives enable mod_deflate for common text types:
# Enable GZIP compression
# Compress HTML, CSS, JavaScript, XML, and fonts
AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/json application/xml font/ttf font/otf image/svg+xml
# Exclude older browsers that choke on compression
BrowserMatch ^Mozilla/4 gzip1\.0[678] no-gzip older
BrowserMatch ^Mozilla/4\.0[678] no-gzip older
Header append Vary User-Agent env=!older
Long‑term caching of static assets lets repeat visitors load pages instantly. We configure mod_expires to set future expiration dates:
# Enable browser caching
ExpiresActive On
# Images – 1 year
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
# CSS – 1 month
ExpiresByType text/css "access plus 1 month"
# JavaScript – 1 month
ExpiresByType application/javascript "access plus 1 month"
# HTML – 1 day
ExpiresByType text/html "access plus 1 day"
ETags can cause unnecessary validation requests on some CDNs. We disable them with the following directive:
# Disable ETags
Header unset ETag
FileETag None
Keep‑Alive reduces the overhead of establishing new TCP connections for each request. While this is often a server‑level setting, we can reinforce it with a header:
# Encourage persistent connections
Header set Connection "keep-alive"
We want all visitors to reach the same canonical URL. If you haven’t set up SSL yet, start with our Joomla SSL certificate guide. The following rules enforce HTTPS and a single www/non‑www version:
# Force HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Force www (uncomment if you prefer www)
# RewriteCond %{HTTP_HOST} !^www\. [NC]
# RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Force non‑www (uncomment if you prefer non‑www)
# RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
# RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
We keep only the rule that matches our preferred domain style.
When we rename an article or move a folder, a permanent redirect preserves SEO value. Example:
# Redirect old article URL to new one
Redirect 301 /old-article.html /new-article.html
# Redirect an entire folder
RedirectMatch 301 ^/old-folder/(.*)$ /new-folder/$1
To prevent other sites from using our bandwidth, we block requests that do not originate from our domain:
# Hotlink protection
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?yourdomain\.com/ [NC]
RewriteRule \.(jpe?g|png|gif|svg|webp)$ - [F,NC]
Replace yourdomain\.com with our actual domain.
Joomla 5 introduced several updates to the default htaccess.txt file:
mod_security false positives.Content‑Security‑Policy header (disabled by default).We should review the new file after an upgrade and merge our custom rules accordingly.
When we move from Joomla 4 to Joomla 5, the following points deserve attention:
RewriteEngine On. Ensure the line is not duplicated.memory_limit. We can set it in .htaccess if the host permits:# Increase PHP memory limit (if allowed)
php_value memory_limit 256M
Security Headers – The default file now includes a basic CSP comment. If we already have a custom CSP, we should keep only one version to avoid conflicts.
A 500 error usually means Apache cannot parse a directive. Steps to resolve:
RewriteEngine On appears only once.mod_rewrite, mod_headers, and mod_deflate are enabled on the server.# and reload the page to isolate the problematic rule.If friendly URLs return 404 errors, we should verify the following:
RewriteEngine On is present and not overridden later in the file.AllowOverride All is set for the Joomla root in the main Apache configuration (often a host‑level setting).mod_rewrite is loaded: apachectl -M | grep rewrite (or ask the host).configuration.php setting $sef = 1; and $sef_rewrite = 1; are enabled.Some hosts place global directives in a parent .htaccess or the main httpd.conf. If we see unexpected redirects or header overrides, we should:
Header set lines that may conflict.Header always set to ensure our rule takes precedence.<IfModule> blocks to avoid errors when a module is missing.The .htaccess file resides in the Joomla root directory, the same folder that contains index.php, configuration.php, and the templates folder. By default it is named htaccess.txt and must be renamed to .htaccess to become active.
htaccess.txt is a plain‑text template shipped with Joomla. It contains commented‑out directives and explanations. When we rename it to .htaccess, Apache reads the file and applies the directives. The content is otherwise identical; the only change is the filename and the fact that the file becomes hidden on Unix‑like systems.
Yes, an incorrect rule can cause 500 errors, prevent SEF URLs, or block legitimate traffic. We recommend editing the file in small steps, testing after each change, and keeping a backup of the original version.
Joomla 5 ships with an updated htaccess.txt that includes new comments and optional security headers. The core directives remain compatible with earlier versions, but we should review the new file after an upgrade and merge our custom rules to avoid duplication.
We can restore the original configuration by:
htaccess.txt from the Joomla installation package..htaccess (overwriting the existing file).Keeping a copy of our custom additions in a separate file (e.g., custom-htaccess.txt) makes future resets easier.
By mastering the joomla htaccess file we gain fine‑grained control over both security and performance without touching Joomla’s core. The steps outlined above—activating the file, adding hardening rules, enabling compression and caching, and handling redirects—provide a solid baseline for any Joomla site. As we upgrade to Joomla 5, we simply merge the new default directives with our existing customizations. Regular testing and a disciplined backup routine keep the site stable while we reap the benefits of faster page loads and a reduced attack surface.