To redirect HTTP to HTTPS in an Apache web server, you need to configure a virtual host file to perform the redirection. First, make sure your Apache server has mod_rewrite enabled. Then, add the following lines to the virtual host configuration file for the HTTP site:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This code will redirect all incoming HTTP requests to HTTPS.