internetjason
internetjason

Reputation: 81

htaccess redirect NOT working for HTTPS - 400 Bad Request Apache 2.4.6

I have just installed SSL certs on a variety of sites. They work fine if I go directly to the https version of the site, but when I go to the http version, I get: "Reason: You're speaking plain HTTP to an SSL-enabled server port."

This is what SHOULD work but does NOT...

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Nor does any version of it

!=on =80 !=443, etc.

I even tried putting this in the vhost.conf file on the server.

My developers and I are out of ideas and we, shockingly, cannot find anything with the almighty Google to help us. Anyone have any thoughts?

Upvotes: 2

Views: 4196

Answers (4)

internetjason
internetjason

Reputation: 81

Ok, so in case others come across this issue, I wanted to update now that I've finally fixed. For us, we had some other stuff in our vhost.conf file that was interfering. Once I wiped it out and took some of this other advice, this is the code that ended up working in vhost.conf. The .htaccess file now has nothing in it as it is not needed.

## -- VIRTUAL HOSTS -- ##
NameVirtualHost *:80

<VirtualHost *:80>
        ServerName dev.example.net
        Redirect permanent / https://dev.example.net/
</VirtualHost>

<VirtualHost *:443>
        #-SERVER CONFIG-#
        ServerAdmin [email protected]
        ServerName dev.example.net
        ServerAlias dev.example.net
        DocumentRoot /var/www/html/example

        #-SSL-#
        SSLEngine On
        SSLProtocol all -SSLv2
        SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
        SSLCertificateFile /etc/httpd/conf/ssl.crt/...
        SSLCertificateKeyFile /etc/httpd/conf/ssl.key/...
        SSLCACertificateFile /etc/httpd/conf/ssl.crt/...
        SSLCertificateChainFile /etc/pki/tls/certs/...
        SSLCACertificateFile /etc/pki/tls/certs/...

        #-LOGGING-#
        ErrorLog /var/www/html/example/error_log

</VirtualHost>

Upvotes: 1

regilero
regilero

Reputation: 30536

I think your problem comes very early, even before mod_rewrite is applied on the request.

Your VirtualHost listening on port 80 is an https virtualhost, but browsers are trying to speak plain http on port 80, and that does not work.

On Apache SSL is activated with :

SSLEngine on

This instruction should only be activated for your Virtualhost listening on *:443 (or any variations of something:443).

You should add some Virtualhost listening on port 80, supporting a bunch of ServerName and ServerAlias that could be used on that server (or maybe all the names, by ensuring this Virtualhost is the default one for port 80), and whose only job is to redirect on port 443.

here you can use links provided by @Anand Bhat to perform this task (and mod_rewrite is not needed).

But all theses 'redirect to https' tasks assume that you already have a working Virtualhost where https is not activated. Because if https is activated everywhere you cannot even start a discussion with the server to receive a redirection, there're no 'plain http' canal to receive this response or even to start asking for something.

Upvotes: 0

Anand Bhat
Anand Bhat

Reputation: 5819

Have you tried these from the Apache HTTPD wiki?

https://wiki.apache.org/httpd/RedirectSSL

https://wiki.apache.org/httpd/RewriteHTTPToHTTPS

Upvotes: 1

Gedao
Gedao

Reputation: 1058

I am using these rewrite rules to redirect my http request to https on my application with SSL certs.

RewriteEngine Off
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]

Upvotes: 0

Related Questions