4
<VirtualHost *:82>
SSLEngine on
SSLCertificateFile /etc/ssl/certs/cert.pem
DocumentRoot "/var/www/site"
<Directory "/var/www/site">
allow from all
Options -Indexes
</Directory>
</VirtualHost>

This is my virtual host config. Its working ssl on port 82.

My problem is, when i try to get this page with http its return error page like:

Bad Request

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.
Hint: https://localhost:82/

So, i just want to redirect http to https on port 82. I try to add :

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

but its not working since it require http first.

How can i do this?

Thank you.

Cem
  • 51
  • 1
  • 1
  • 3
  • 1
    I don't know if you can do what you want. Its possible to have a redirect for a http site to a https site, like you have configured. But they would be on different ports. – becomingwisest Aug 01 '12 at 21:40
  • I know that you can redirect 80 to 443 ssl, those are different ports. But in my case i want to serve everything on this port (82) via ssl. Maybe its possible to do it with playing custom error page since its showing an error page when i use http on this port but i don't know how. – Cem Aug 01 '12 at 21:46
  • 1
    I don't think you can do this by rewrite either. As you are thinking, using custom 400 error page to redirect works. Try add `ErrorDocument 400 https://localhost:82/` – mask8 Aug 01 '12 at 22:02
  • Tried already, weird but its show another error page : "Found The document has moved here: `https://localhost:82/`" – Cem Aug 01 '12 at 22:08
  • How about `ErrorDocument 400 /redirect.html` and you create the redirect.html that actually redirects to your SSL page by meta tag or js. In my local env, it seems to work – mask8 Aug 01 '12 at 22:19
  • that /redirect.html require ssl too so its gave same error bad request. i need https to see that redirect.html. – Cem Jul 23 '13 at 07:33

2 Answers2

2

I know that you can redirect 80 to 443 ssl, those are different ports. But in my case i want to serve everything on this port (82) via ssl. Maybe its possible to do it with playing custom error page since its showing an error page when i use http on this port but i don't know how

You can't host both encrypted and plain-text over the same port. If someone connects to the plain-text port say port 80 or 81, then you can forward them to your HTTPS port say 82 in this example. So something like this should do the trick:

# Plain-text rewrite:
<VirtualHost *:81>
DocumentRoot "/var/www/site"
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}:82%{REQUEST_URI}
<Directory "/var/www/site">
allow from all
Options -Indexes
</Directory>
</VirtualHost>

# SSL config
<VirtualHost *:82>
SSLEngine on
SSLCertificateFile /etc/ssl/certs/cert.pem
DocumentRoot "/var/www/site"
<Directory "/var/www/site">
allow from all
Options -Indexes
</Directory>
</VirtualHost>

In this case any user connecting to port 81 would get forwarded to port 82. Anyone connecting to port 82 will be over SSL.

Andrew Case
  • 3,489
  • 3
  • 23
  • 39
  • Thank you for answer, actually this is working but i'm just looking if is there any more elegant way to do this without require another virtual host configured on another non-ssl port to do this.I don't understand why apache need another host to do this job, i mean there is a site require https, apache show error when you ask page via http means apache can publish something for that https required host, why not just redirect to https and publish the site itself? There should be an option about it. – Cem Jul 23 '13 at 07:25
  • "> You can't host both encrypted and plain-text over the same port." not with apache, thats right. but you can do it with nginx – that guy from over there Sep 13 '13 at 08:06
  • oh, and dont forget to use HSTS - Headers – that guy from over there Sep 13 '13 at 08:06
1

In httpd-ssl file, with 8085 is my custom port, add "ErrorDocument 400 https://your_website:8085/" to Vitrual host config

<VirtualHost _default_:8085>
    ErrorDocument 400 "https://your_website:8085/"
    ...