title: Apache RewriteRule to remove port on any domain name
body: <p>I have a virtual server with one IP address, serving a few different sites. One of them has an SSL certificate on it. </p>
  
  <p>I needed to add an SSL cert on a second domain for private use by myself, and as I only have one IP address, I added it on port 8080 instead. This works fine. </p>
  
  <p>My problem now is that all the domains that point to the server will display my private site if port 8080 is requested on that domain. </p>
  
  <p><code>
  https://example1.com:8080/ -&gt; will show my private site, but shouldn't
  https://example2.com:8080/ -&gt; will show my private site, but shouldn't
  https://desired-domain.com:8080/ -&gt; will show my private site, this is correct!
  </code></p>
  
  <p>Anyone visiting <code>https://example1.com:8080/</code> should be redirected to <code>http://example1.com/</code>, and the same for <code>example2.com</code>. </p>
  
  <p>I tried... </p>
  
  <p><code>
  RewriteCond %{HTTP_HOST} !desired\-domain\.com [NC]
  </code></p>
  
  <p><code>
  RewriteRule ^(.*)$ http://%{HTTP_HOST}$1 [L,R=301]
  </code></p>
  
  <p>...but the port is appended to HTTP_HOST, so it comes out as <code>http://example1.com:8080/</code></p>
  
  <p>So, in short, my question: how can I redirect to the requested host, but ignore the request port?</p>
  
  <hr>
  
  <p><em><strong>EXTRA</em></strong></p>
  
  <p>I've tried this... </p>
  
  <p><code>
  RewriteCond %{HTTP_HOST} !(desired-domain.com):8080$ [NC]
  </code></p>
  
  <p><code>
  RewriteRule ^(.*)$ http://%1$1 [L,R=301]
  </code></p>
  
  <p>...but it redirects to <code>http://localhost/</code>. That's better than showing the wrong site's page, though, as nobody should be requesting port 8080 on any other domain anyway! </p>
  
  <p>I suspect there's a better 'desired-domain.com' regex, but my regex-mojo isn't flowing today.</p>