7

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.

My /etc/apache2/sites-available/default file is as follows:

    <VirtualHost *:80>
        RewriteEngine On
        RewriteCond %{SERVER_PORT} !^443$
        RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
    </VirtualHost>

The HTTPS host is defined in default-ssl in the same directory.

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.

To further clarify, the following are valid links:

    https://website:5443
    https://192.168.200.80:443

The redirect works here:

    http://192.168.200.80

But the following gives a 400 Bad Request, and this is where the redirect is needed:

    http://website:5443/

"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."

dutchgold92
  • 125
  • 1
  • 1
  • 6
  • This seems like the long way to do a simple thing, maybe you have a reason for it though. In anycase, you should check out mod_proxy: http://httpd.apache.org/docs/2.0/mod/mod_proxy.html along with this relative SO question: http://stackoverflow.com/questions/1162375/apache-port-proxy. – Craig van Tonder Oct 23 '16 at 17:56

4 Answers4

11

This is totally possible. The following redirects all http to the https url.

<VirtualHost *:80>
    ServerName   mydomainname.com
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

make sure you load the rewrite module mod_rewrite and enable it.

Steven Soroka
  • 19,404
  • 4
  • 52
  • 40
3

Your problem here is the initial HTTP request: This won't work as the server won't understand it receiving the request on port 443 (as the response code suggests).

If no port is given, the protocol http defaults to port 80, https to port 443.

This is also the reason why your local redirect works. I bet, if you access the page through http://website/ (with proper port forwarding of port 80), it will work as well. Also note that your VirtualHost is only defined for port 80 anyway, so it won't be valid for requests sent to website:5443 (or website:443).

In general, you'd need a server accepting both HTTP and HTTPS requests on a single port. Not sure any popular server actually supports something like that, because (I think) it essentially violates the specs.

Mario
  • 35,726
  • 5
  • 62
  • 78
  • Not that I'm aware of. You'd probably need a custom server somehow accepting both or a custom proxy redirecting requests based on their actual content. – Mario Jul 25 '13 at 11:04
  • Well, in either case it's not really worth it in this situation. Cheers. – dutchgold92 Jul 25 '13 at 13:45
1
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
0

if u want to redirect your site from http:// anything.example.com to https: //anything.example.com ... Just create a dedicated hosting .conf file as /etc/httpd/conf.d/dedicated.conf and other conf file as virtual.conf ... entries for dedicated.conf are as follows....

this is dedicated server hosting conf file for redirecting it to https... th[][1]

<virtualhost *:80>
servername host.example.com
documentroot /var/www/html
rewriteengine on
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
sslcertificatefile /etc/pki/tls/certs/name.crt
sslcertificatekeyfile /etc/pki/tls/private/name.key
</virtualhost>
<directory /var/www/html>
allowoverride all
require all granted
</directory>

Alternatively as mentioned in comment below, we can use redirect also:

<virtualhost *:80>
servername host.example.com
documentroot /var/www/html
RedirectMatch / https://host.example.com:ANY_PORT/ #if there is specific port
sslcertificatefile /etc/pki/tls/certs/name.crt
sslcertificatekeyfile /etc/pki/tls/private/name.key
</virtualhost>
<directory /var/www/html>
allowoverride all
require all granted
</directory>
Lokendra Singh Rawat
  • 1,889
  • 1
  • 9
  • 8