Skip to content

Debugging mod_rewrite rules in cPanel .htaccess files

How to surface the actual rewrite decisions Apache is making on a cPanel host using LogLevel rewrite:trace3, condition tracing, and the rewrite-test approach to per-vhost rules.

A WordPress site is returning 404 for every URL except the home page. The .htaccess looks textbook. The rules pass httpd -t. Yet RewriteRule . /index.php [L] is silently failing to match. The fix is not to guess at the regex — it is to turn on rewrite tracing and read the actual decisions Apache is making against the actual request URI.

What’s happening

mod_rewrite evaluates rules in order, against either the URL-path or the file path depending on the context. Inside a .htaccess file, the rules see the path relative to the directory the .htaccess lives in, with the leading slash stripped. A rule that looks like ^/(.*)$ index.php will never match in .htaccess context — that leading slash is gone. This is the single most common cause of “the rule does not match.” Tracing makes it visible in 30 seconds.

How to verify

Enable rewrite tracing for the vhost:

apachectl -M | grep rewrite
apachectl -t -D DUMP_INCLUDES | grep example.com
grep -rn "RewriteRule\|RewriteCond" /home/<user>/public_html/.htaccess
cat /var/cpanel/userdata/<user>/example.com.cache 2>/dev/null

Add a temporary trace directive via WHM > Apache Configuration > Include Editor > Pre VirtualHost Include (scoped to the domain):

<Directory "/home/<user>/public_html">
    LogLevel rewrite:trace3
</Directory>

Reload Apache and watch the per-vhost error log.

The fix

  1. With trace enabled, hit the broken URL once and read the per-vhost error log. The trace shows, line by line: the URI Apache started with, each rule and condition evaluated, what each substitution produced, and which [L] or [END] terminated the chain.
  2. The decisive line is the first ^ regex anchor in .htaccess context — if it has a leading slash, the rule will never match. Remove the slash: ^index\.php$ not ^/index\.php$.
  3. The second common cause is RewriteCond %{REQUEST_FILENAME} !-f failing because cPanel’s mod_userdir or default index file resolution is shimming a virtual file. Confirm with RewriteCond %{REQUEST_FILENAME} !-f [OR] followed by an explicit case.
  4. After fixing, remove the LogLevel rewrite:trace3 line. Leaving it on in production fills the error log fast — a single page request generates dozens of lines.
  5. If the .htaccess is generated by an application (WordPress, Magento), do not edit it permanently — the app will overwrite it on the next configuration save. The fix belongs in the application’s own rewrite settings or in a Pre VirtualHost Include you control.
  6. After the fix, the trace approach stays in the toolkit. Any future rewrite question is solved by re-enabling trace, hitting the URL, reading the log.

For environments where rewrite issues are a recurring class of ticket, Stack Harbor uses trace-driven diagnosis as part of WHM server management.