Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Apache returns 403 when a URL contains %3F

Error403 Forbidden

Short answer

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?. The usual front-controller rule trips this. Fix the client first so it stops encoding the ? that separates the query. If you cannot, add the UnsafeAllow3F flag to that one rule.

  • Applies to Apache httpd 2.4, PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Send the same request with a plain ? and with %3F and compare the status. If only the encoded one gets 403, this page applies.

    curl -s -o /dev/null -w '%{http_code}\n' 'https://example.com/page?name=test'
    curl -s -o /dev/null -w '%{http_code}\n' 'https://example.com/page%3Fname=test'
  2. 2Match what you see, then jump to its fix

    1. 1The access log shows %3F where the query string should startThe client encoded the whole URL, including the ?Fix
    2. 2403 only on URLs with %3F, and the site uses RewriteRule ... index.php?route=$1A rewrite rule puts the path into a query and meets %3FFix

What does the error look like?

  • 403 Forbidden
  • a request like /page%3Fname=test is refused while /page?name=test works

Cause 1 of 2

The client encoded the whole URL, including the ?

1Check

rawurlencode() turns every character outside A-Z a-z 0-9 - _ . ~ into a percent sequence, so passing a complete URL through it also encodes the ? and the = signs. The manual says to encode only the parts, such as one query value.

grep '%3F' /var/log/apache2/access.log | tail -n 5
grep -rn 'rawurlencode\|urlencode' src/ | head

2Fix

Encode each value on its own, then join with literal ?, = and &, or let http_build_query() build the query string.

$url = 'https://example.com/page?'
     . 'name=' . rawurlencode($name);
// or
$url = 'https://example.com/page?' . http_build_query(['name' => $name]);

3Verify

The request now carries a real ? and the status is 200.

curl -s -o /dev/null -w '%{http_code}\n' 'https://example.com/page?name=test'

Confirmed by php.net

Did this fix it?

Cause 2 of 2

A rewrite rule puts the path into a query and meets %3F

1Check

The Apache flag documentation says a rewrite is refused when the request has %3F and the substitution contains a literal ?. This protects against a capture being re-used to inject query parameters. A front-controller rule is the textbook case.

grep -rn 'RewriteRule' /etc/apache2/sites-enabled/ /var/www/html/.htaccess

2Fix

Preferably change the URLs so no %3F is sent. If you must accept them on this one rule, add the UnsafeAllow3F flag, and only to a rule you have checked.

RewriteCond "%{REQUEST_FILENAME}" !-f
RewriteCond "%{REQUEST_FILENAME}" !-d
RewriteRule "(.+)" "index.php?route=$1" [L,QSA,UnsafeAllow3F]

3Verify

Test the config, reload, and repeat the failing request.

sudo apachectl configtest && sudo systemctl reload apache2
curl -s -o /dev/null -w '%{http_code}\n' 'https://example.com/page%3Fname=test'

Confirmed by httpd.apache.org

Did this fix it?

How do I stop it coming back?

  • Build URLs from parts: encode values one by one, never the finished URL.
  • Add UnsafeAllow3F to a single reviewed rule, never to every rule in the file.
  • Keep Apache updated; the check exists to close a security hole.

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?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

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