Short answer
──────────────
No—at least, not reliably.
Long(er) answer
────────────────
What .htaccess actually sees
• A plain .htaccess file only has access to Apache-level modules such as mod_rewrite, mod_headers, etc.
• mod_rewrite can inspect the URL path and query-string (i.e. the GET part) but cannot look inside the request body where POST parameters live.
• To see POST data you need something like mod_security (a Web Application Firewall module) or another specialised filter that is not enabled on most shared hosts by default.
Why you shouldn’t rely on it anyway
• SQL-Injection happens inside your application when you concatenate untrusted data into a SQL statement.
• Blocking specific keywords ( union , select , 1=1 , etc.) is both brittle (attackers obfuscate) and noisy (false positives for legit words like “select date”).
• The only bullet-proof fix is at the application layer:
– Use prepared / parameterised statements (PDO, mysqli, pg-prepare, etc.).
– Validate / sanitise input according to expected type/format.
– Use proper DB permissions (app user can’t DROP tables).
If you do have mod_security
You can enable basic generic rules in .htaccess (or, better, the main Apache config):
apache
<IfModule mod_security.c>
SecFilterEngine On
SecRequestBodyAccess On # This is what lets it see POST
SecDefaultAction "deny,log,status:403"
# Very naive examples
SecRule ARGS "@rx (\bor\b|\band\b).*?=\d+--" "id:1001,phase:2,deny,msg:'SQLi attempt'"
SecRule ARGS "@rx union.*select" "id:1002,phase:2,deny,msg:'SQLi attempt'"
</IfModule>
Pros: catches some automated scanners.
Cons: needs constant tuning; can still be bypassed; adds processing overhead.
If you only have mod_rewrite (.htaccess)
You can block obvious query-string attacks but not POST bodies:
apache
# Still only acts on GET parameters!
RewriteEngine On
RewriteCond %{QUERY_STRING} "(?i)(union([^a-z]|$).*select|select[^a-z].*from|drop[^a-z]+table)" [OR]
RewriteCond %{QUERY_STRING} "(?i)information_schema|sleep\(|benchmark\("
RewriteRule ^ - [F,L]
This helps reduce noise in your logs but is not a SQL-i defence.
Take-away
─────────
Treat .htaccess as a band-aid for very noisy attacks, not as real SQL-Injection protection. Fix the code first, add a WAF if you need an extra safety net, and leave keyword-blocking in .htaccess as a last-ditch measure (or don’t bother at all).
User
can .htaccess to block some string from HTTP request body?
Assistant
Short version
A plain “.htaccess + mod_rewrite” configuration cannot look inside the HTTP-request body (POST/PUT payload), so it cannot block or rewrite based on a string that appears only there.
The only time a .htaccess file can do this is when an extra module that does expose the body (e.g. mod_security, ModSecurity 2/3, or a custom input filter) is already loaded on the server and configured to allow per-dir rules. In most shared-hosting setups that module simply isn’t present, so the answer is effectively “no”.
Why vanilla .htaccess can’t see the body
The classic tools you have in .htaccess are
• mod_rewrite (URL path, query string, headers)
• SetEnvIf, mod_headers, etc. (headers)
None of those modules ever read the entity body of the request, mainly for performance reasons. By the time Apache finishes parsing .htaccess, it hasn’t yet consumed the body.
Because of that, variables such as %{QUERY_STRING} or %{REQUEST_URI} exist, but there is no%{REQUEST_BODY} (unless you install a third-party module).
What you could use if the host provides it
mod_security (ModSecurity) – full WAF that does inspect the body.
Example in .htaccess (only works if the module is enabled and allows per-dir rules):
apache
<IfModule mod_security2.c>
SecRequestBodyAccess On
# Block requests whose body contains the word "foobar"
SecRule REQUEST_BODY "@contains foobar" "id:9001,phase:2,deny,status:403,msg:'forbidden string in body'"
</IfModule>
custom modules – there are niche filters (e.g. mod_block_post or Lua scripts with mod_lua) that admins sometimes compile in, but again that’s outside normal shared hosting.
Recommended way to filter “bad” content
Application layer: Validate/parse the input in your code.
Framework / ORM: Use parameterised queries, strict schemas, etc.
Server/WAF (optional): Ask the hosting provider whether they already run ModSecurity or provide a reverse-proxy WAF (Cloudflare, AWS WAF, etc.).
Take-away
• .htaccess by itself ⇒ cannot touch the request body.
• You need a dedicated WAF module (like ModSecurity) or to handle it in your application code.
If you’re on shared hosting and don’t control the global Apache modules, the practical answer is: filter the data in your application or move in front of a WAF/CDN.