Using mod_rewrite, how can I reject (by returning a 404) all URLs whose QUERY_STRING
starts with a hashtag (#
)? For example, these URLs are all examples of what I want to reject:
https://example.com/?#something
https://example.com/?#anything-else
https://example.com/?#i-don't-care-about-this-text
But if the string after the ?
begins with anything else, or if there's no query string at all, I want it to pass through.
I know the hashtag is a fragment identifier and that strings after the hashtag aren't something you can process with mod_rewrite, but I was expecting the hashtag itself to show up. So I've tried rules to match like these:
RewriteCond %{QUERY_STRING} #
RewriteCond %{QUERY_STRING} "#"
RewriteCond %{QUERY_STRING} (#)
RewriteCond %{QUERY_STRING} -n
RewriteCond %{QUERY_STRING} ^$
But none of these seem to catch it. Can you suggest a way to detect and reject URLs whose QUERY_STRING
starts with a hashtag?
UPDATE: But if a request comes in for https://example.com
(with no ?
) it should pass through.
Another idea I've tried is:
RewriteCond %{REQUEST_URI} ^\/\?$
RewriteCond %{QUERY_STRING} =""
RewriteRule ^ - [L,R=404]
What I'm trying to do here is say: if REQUEST_URI
contains a ?
and if QUERY_STRING
is empty, then redirect to 404. But it redirects everything (including URLs with non-empty query_strings) to 404.
You can try this rule to detect a ?
with no query string:
RewriteEngine On
RewriteCond %{THE_REQUEST} \?\s
RewriteRule ^$ - [L,R=404]