2

I am currently using Apache mod_rewrite to redirect to HTTPS, but I would like to remove the body from the response.

How it is currently:

Current response

Example: How I would like it to be

Desired response

My redirect configuration:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
reddot2k
  • 31
  • 3

1 Answers1

1

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.

MrWhite
  • 12,647
  • 4
  • 29
  • 41
  • 1
    Hello, thank you very much for your great answer. I tried the version with the empty file, sadly without success (I got an additional 500 Internal Server Error), but the single-character version worked. However I am now leaving the body in there since there is no real need to remove it, I just wanted to know if I could remove it. Thank you for all your effort. – reddot2k Apr 05 '18 at 15:21