Score:0

virtual host / apache2 / ssl configuration - auto redirect from 80 to 443 not working - bad request 400 error

ps flag

This problem has cost me a few hours trying to fix and I simply have no clue - so huge thanks in advance to anyone that can help me solve this.

This is the Bad Request 400 error I get:

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.

And this is my vhosts file:

<Directory /var/www/sub1>
   Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

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

</VirtualHost>

<VirtualHost *:443>
    ServerName sub1.site1.com
    DocumentRoot /var/www/sub1
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/sub1.site1.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/sub1.site1.com/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/sub1.site1.com/chain.pem
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Here's the strange part - as long as I specify port 80 at the end of the URL - it works fine. If I DON'T type in http:// at the front of the URL - it works fine. -BUT- If I type in http://sub1.site1.com - it throws the above error.

I'm not sure if my apache2 config or the fact that I used certbot on my ubuntu server has something to do with this odd error, but I've tried everything I can think of to fix it.

So why would:

sub1.site1.com
sub1.site1.com:443
https://sub1.site1.com
http://sub1.site1.com:80

All work just fine, but

http://sub1.site1.com 

Throw the above error rather than just automatically redirecting to one of the 4 url's that work fine?

John Hanley avatar
in flag
Please review how other people format details in their posts. Then update your post.
John Hanley avatar
in flag
Are you entering the URL in a browser or via a CLI tool? If a CLI tool show the entire command.
John Hanley avatar
in flag
Run the following command. Post the command and output in your post. `curl -v -L http://sub1.site1.com`.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.