Apache returns 403 when a URL contains %3F
403 ForbiddenShort 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.
Fix it fast
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'2Match what you see, then jump to its fix
What does the error look like?
403 Forbiddena 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/ | head2Fix
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
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/.htaccess2Fix
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
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
Back end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fixBack end
You switched PHP versions with Homebrew, but php -v still shows the old one
brew link php@8.3 prints an error or does nothing
Homebrew can have several PHP versions installed, but only one is linked into the prefix, and your shell uses the first php on PATH.
3 causes, each with a fix