title: Redirecting HTTP to HTTPS with Apache
body: <p>I have an issue using mod_rewrite to force redirection of HTTP requests to HTTPS using Apache 2.2.22 on Ubuntu Server 12.04.</p>
  
  <p>My /etc/apache2/sites-available/default file is as follows:</p>
  
  <pre><code>    &lt;VirtualHost *:80&gt;
          RewriteEngine On
          RewriteCond %{SERVER_PORT} !^443$
          RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
      &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>The HTTPS host is defined in default-ssl in the same directory.</p>
  
  <p>Visiting the server's local IP address, the redirect appears to work fine. However, accessing it via the FQDN, it doesn't. Using the FQDN, the site is available at port 5443, which is mapped in the firewall to 443 on the server, so perhaps that has something to do with the problem. I cannot just use port 443 directly, as it is in use on this IP address by another server.</p>
  
  <p>To further clarify, the following are valid links:</p>
  
  <pre><code>    https://website:5443
      https://192.168.200.80:443
  </code></pre>
  
  <p>The redirect works here:</p>
  
  <pre><code>    http://192.168.200.80
  </code></pre>
  
  <p>But the following gives a 400 Bad Request, and this is where the redirect is needed:</p>
  
  <pre><code>    http://website:5443/
  </code></pre>
  
  <p>"Your browser sent a request that this server could not understand.
  Reason: You're speaking plain HTTP to an SSL-enabled server port.
  Instead use the HTTPS scheme to access this URL, please."</p>