Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a virtual host that works with both http and https?

My config is:

Listen 443 http
NameVirtualHost *:80
NameVirtualHost *:443
ServerName *:80

<VirtualHost *:80> 
  [some non-ssl stuff]
  ServerName account.example.com
</VirtualHost>

<VirtualHost *:443> 
  [some non-ssl stuff(directory, docroot)] 
  ServerName account.example.com
  SSLEngine on
  SSLCertificateFile /Users/myusername/certs/server.crt
  SSLCertificateKeyFile /Users/myusername/certs/server.key
</VirtualHost>

So I can't access the http version of my site, but ssl version is working correctly. I want to use both vhosts, http(80) and https(443) to rewrite http to https URL via mod_rewrite.

uname -a
Linux 3.4.62-53.42.amzn1.x86_64 GNU/Linux

httpd -v
Server version: Apache/2.2.25 (Unix)

Please help to understand what I'm doing wrong.

like image 972
Clyde Avatar asked Nov 08 '13 06:11

Clyde


1 Answers

So, my config now is:

Listen 443 http
Listen 80
NameVirtualHost *:80
NameVirtualHost *:443
ServerName *:80

<VirtualHost *:443> 
  [some non-ssl stuff(directory, docroot)] 
  ServerName account.example.com
  SSLEngine on
  SSLCertificateFile /Users/myusername/certs/server.crt
  SSLCertificateKeyFile /Users/myusername/certs/server.key
</VirtualHost>

<VirtualHost *:80>
  SSLEngine off
  [other stuff like docroot]
</VirtualHost>

Not sure about SSLEngine off, but now it works. So, I add rewrite rule to redirrect from http to https in http vhost's .htaccess file:

#Redirrect from http to https
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
like image 155
Clyde Avatar answered Oct 26 '22 20:10

Clyde