Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Apache ignores your .htaccess file

ErrorRedirect and rewrite rules in .htaccess have no effect

Short answer

Apache ignores .htaccess when AllowOverride is None, which is its default. Add AllowOverride All (or FileInfo for rewrites) inside the Directory block of your site and reload. If rewrites still fail, turn on RewriteEngine and make sure the module is loaded.

  • Applies to Apache 2.4, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Put one nonsense word in the .htaccess file and load the page. A 500 error means the file is read. A normal page means Apache ignores it.

    echo "TestMe" >> /var/www/example/.htaccess
    curl -I http://example.com/
  2. 2Match what you see, then jump to its fix

    1. 1The test word gives no error and the page loads as before.AllowOverride is None for that directoryFix
    2. 2The error log says a directive is "not allowed here".AllowOverride is on, but too narrow for the directive you usedFix
    3. 3Other .htaccess directives work, but RewriteRule lines do nothing.Rewrite rules need the engine on and FollowSymLinksFix

What does the error look like?

  • Redirect and rewrite rules in .htaccess have no effect
  • 500 Internal Server Error after editing .htaccess
  • .htaccess: DirectoryIndex not allowed here

Cause 1 of 3

AllowOverride is None for that directory

1Check

The default is AllowOverride None, which makes Apache skip .htaccess files. Look for the setting in your site config.

grep -R -n -A3 "<Directory" /etc/apache2/ | grep -i -E 'Directory|AllowOverride'

2Fix

In the Directory block that covers your site, allow overrides, then reload Apache.

<Directory "/var/www/example">
    AllowOverride All
</Directory>

3Verify

Test the config, reload, and repeat the nonsense-word test. It must now return 500. Remove the word afterwards.

sudo apachectl configtest && sudo systemctl reload apache2
curl -I http://example.com/

Confirmed by httpd.apache.org

Did this fix it?

Cause 2 of 3

AllowOverride is on, but too narrow for the directive you used

1Check

Each directive belongs to an override group. RewriteRule, Redirect, Header and ErrorDocument need FileInfo. Read the log line, then the directive's Override entry in the docs.

sudo tail -n 20 /var/log/apache2/error.log

2Fix

Grant the group the directive needs, or list single directives with AllowOverrideList.

<Directory "/var/www/example">
    AllowOverride FileInfo
    # or: AllowOverride None
    #     AllowOverrideList Redirect RedirectMatch
</Directory>

3Verify

Reload and repeat the request. The 'not allowed here' line must stop appearing.

sudo apachectl configtest && sudo systemctl reload apache2

Confirmed by httpd.apache.org

Did this fix it?

Cause 3 of 3

Rewrite rules need the engine on and FollowSymLinks

1Check

Rewriting is off by default (RewriteEngine off) and, in .htaccess, also needs FollowSymLinks or SymLinksIfOwnerMatch allowed in the server configuration.

apachectl -M | grep rewrite
head -n 5 /var/www/example/.htaccess

2Fix

Load the module, set the engine on in the .htaccess file, and permit the options in the Directory block.

# server config
LoadModule rewrite_module modules/mod_rewrite.so
<Directory "/var/www/example">
    AllowOverride FileInfo
    Options +FollowSymLinks
</Directory>

# .htaccess
RewriteEngine On
RewriteRule ^old-page\.html$ /new-page.html [R=301,L]

3Verify

Reload Apache and request the old URL.

curl -I http://example.com/old-page.html

Confirmed by httpd.apache.org

Did this fix it?

How do I stop it coming back?

  • Keep rules in the main server config when you can; Apache reads it once at start instead of on every request.
  • Change one directive at a time and read the error log after each reload.
  • Allow only the override group you need, not All, on shared servers.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Servers

dnf cannot install MySQL on Amazon Linux 2023

dnf cannot find a mysql-server package on Amazon Linux 2023

Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.