You can define a custom ErrorDocument for the 301 response, in which you can set an empty response. (Although specifying a custom "error document" for a non-error, ie. for anything other than a 4xx or 5xx status, is not explicitly documented.)
For example:
ErrorDocument 301 /errordocs/empty.html
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Where /errordocs/empty.html
is literally an empty document.
Alternatively, you can set the ErrorDocument
to a plain string. But you can't set an entirely empty string, as Apache doesn't then see the second argument and aborts with an error: "ErrorDocument takes two arguments". However, you can reduce this to a single character. For example, to send just a hyphen (-
) in the response body:
ErrorDocument 301 "-"
This does, however, set the response body for all Apache 301 responses. (However, if this is in the VirtualHost container for port 80 in the server config, then this will be restricted to the HTTP-only redirects anyway.)
Reference: This answer to a StackOverflow question goes into more detail about returning an empty response from Apache.
However, as @MichaelHampton pointed out in comments, whether you should be sending an empty response body in the case of a redirect is another matter.
It is in the server config (.conf file)
Aside: If this is in the server config, then you should be using a simple mod_alias Redirect
in the non-HTTPS (port 80) VirtualHost container, instead of mod_rewrite. Using mod_rewrite in this context, to explicitly check the HTTPS
server variable, is unnecessary. This makes no difference to the error document that is returned.