keepcalculator

06 Numbers and Powers

Fix .htaccess Rewrite Rules Not Working in Apache

Card No. 06·15289 · Updated · Standard formula, shown below

If your Apache server is ignoring the rewrite rules in your .htaccess file—returning 404 errors, serving the wrong page, or just not rewriting URLs—this guide walks you through the most common causes and fixes. You’ll check Apache’s configuration, verify that mod_rewrite is enabled, confirm the .htaccess file is being read, and then test and debug your rules step by step.

What You’ll Need

  • Access to your server’s Apache configuration files (httpd.conf, apache2.conf, or the site’s virtual host file).
  • Command-line access (SSH) or a file manager that lets you edit hidden files.
  • Ability to restart or reload Apache (sudo systemctl reload apache2 or sudo service apache2 reload on most Linux systems).
  • A text editor to create or edit .htaccess files.

Step-by-Step

  1. Confirm the .htaccess file is actually being read. By default, Apache allows .htaccess overrides only if the directory’s AllowOverride directive is set to All (or at least FileInfo). If it’s set to None, Apache silently ignores the file. Check your virtual host or main configuration for lines like <Directory /var/www/html> and ensure AllowOverride All is present. After editing, restart Apache.
  2. Verify that mod_rewrite is enabled. Run apache2ctl -M (or httpd -M) and look for rewrite_module in the output. If it’s not listed, enable it with sudo a2enmod rewrite on Debian/Ubuntu, or uncomment the LoadModule rewrite_module line in httpd.conf on other systems. Restart Apache.
  3. Check that the .htaccess file is in the correct directory. The file must be in the document root or in the subdirectory where you expect the rewrite to apply. For example, if your site is at /var/www/html, the .htaccess should be there. If you’re using a subdirectory like /var/www/html/blog, place it there.
  4. Ensure the file is named correctly. It’s exactly .htaccess (no leading dot in the name, no extra extension). Hidden files often aren’t shown by default in file managers—use ls -la in the terminal to confirm.
  5. Test with a minimal rule. Replace your current rules with a simple one to isolate the problem. For example, to redirect all requests to a test page, use:
    RewriteEngine On
    RewriteRule ^test$ test.html [L]

    Then visit http://yourdomain.com/test. If this works, the issue is in your original rules. If it doesn’t, the problem is with the server configuration.

  6. Make sure RewriteEngine is On. Every .htaccess file that uses rewrite rules must start with RewriteEngine On. If it’s missing, no rules will run.
  7. Check for syntax errors. A missing backslash, an unescaped space, or a typo in a regular expression can break the entire file. Look for common mistakes like using ^ incorrectly or forgetting to escape dots in domain names. You can test your regex with an online tool or a local PHP script.
  8. Verify that the file permissions are correct. Apache needs read access to the file. On most Linux systems, 644 (owner read/write, group and others read) is sufficient. If the file is owned by root, make sure the Apache user (usually www-data) can read it. Use chmod 644 .htaccess and chown www-data:www-data .htaccess if needed.
  9. Check for conflicting rules in parent directories. If you have a .htaccess in a parent directory that sets RewriteOptions or has its own RewriteEngine, it can affect subdirectories. The rules are inherited unless you use RewriteOptions InheritDown or explicitly disable inheritance.
  10. Look for other modules that might interfere. Modules like mod_security or custom error handlers can sometimes block or alter requests before mod_rewrite runs. Temporarily disable them to test, but be aware of security implications.
  11. Restart Apache after any configuration change. Some changes (like AllowOverride) require a full restart, not just a reload. Use sudo systemctl restart apache2 or sudo service httpd restart.

Common Mistakes

  • Forgetting to enable RewriteEngine. The most common oversight—without RewriteEngine On, nothing works.
  • Using absolute paths in RewriteRule. The pattern is matched against the URL path, not the filesystem path. For example, ^/index.php won’t match because the leading slash is stripped in .htaccess context.
  • Not escaping special characters. In the substitution or pattern, dots, slashes, and hyphens need to be escaped if they are meant literally. For instance, example.com should be example\.com in a regex.
  • Mixing up RewriteRule and RewriteCond. Remember that RewriteCond applies to the next RewriteRule only. If you put a condition after a rule, it won’t affect it.
  • Putting .htaccess in the wrong directory. The file must be in the directory where the URL is mapped, not necessarily the document root.
  • Using [R] without a status code. A redirect like [R] defaults to 302, which is fine for testing but not for permanent redirects. Use [R=301] for SEO.

Tips

  • Use the L flag to stop processing. If you have multiple rules, add [L] to the last rule you want to apply for a given request. This prevents later rules from rewriting the already-rewritten URL.
  • Test with curl -I. This shows the response headers, including any redirects. For example, curl -I http://yourdomain.com/test will show if you get a 301 or 302 and where it points.
  • Enable rewrite logging. In your virtual host or main config, add:
    RewriteLogLevel 9
    RewriteLog "/tmp/rewrite.log"

    (Note: On Apache 2.4+, use LogLevel alert rewrite:trace6 instead.) Then reload Apache and check the log to see exactly how each rule is processed.

  • Use %{REQUEST_URI} for complex conditions. If you need to match the full URI, it’s safer than using the pattern alone.
  • Keep a backup. Before making changes, copy your working .htaccess to .htaccess.bak so you can quickly revert if something breaks.

Troubleshooting

If the steps above don’t solve the problem, try these additional checks:

  • Check the Apache error log. Look in /var/log/apache2/error.log or /var/log/httpd/error_log for messages about .htaccess or rewrite. Sometimes a syntax error is logged even if the page loads.
  • Verify that the server is actually Apache. If you’re using Nginx or LiteSpeed, .htaccess files are ignored entirely. Check with curl -I and look at the Server header.
  • Try moving the rules to the main config. If .htaccess still doesn’t work, put the same rules directly in the <Directory> block of your virtual host. If they work there, the problem is with .htaccess processing (AllowOverride).
  • Check for a broken symlink. If your document root is a symlink, Apache might not follow it. Add Options +FollowSymLinks in the directory block.
  • Disable other modules temporarily. For example, mod_security can interfere with rewrite rules. Disable it, test, then re-enable if it’s not the cause.
  • Check if the rewrite is being overridden by a later rule. Use the rewrite log to see the full sequence of rules applied to a request.

Frequently Asked Questions

Why does my .htaccess file work on one server but not another?

Different servers have different Apache configurations. The most likely difference is that AllowOverride is set to None on the second server, or mod_rewrite is not enabled. Check both.

Can I use .htaccess on Nginx?

No. Nginx does not support .htaccess files. You need to configure rewrites in the server block using rewrite directives. If you’re on Nginx, that’s why your rules are ignored.

Why do I get a 500 Internal Server Error after editing .htaccess?

This usually means a syntax error in the file. Check for missing spaces, unescaped characters, or an incorrect directive. The error log will often point to the exact line.

How do I redirect all HTTP traffic to HTTPS using .htaccess?

Add this rule at the top of your .htaccess:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

Why is my rewrite rule not working for URLs with query strings?

By default, the RewriteRule pattern only matches the URL path, not the query string. You need to use a RewriteCond to match against %{QUERY_STRING} or include the QSA flag to append the query string to the rewritten URL.

How do I test a rewrite rule without affecting my live site?

Create a test subdirectory with its own .htaccess and a simple rule, then test there. Or use a local development environment like XAMPP or a Docker container with Apache.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →