Jump to content
Sign in to follow this  
NotionCommotion

Why aren't I redirecting to HTTPS?

Recommended Posts

Hoping to get another set of eyes.  Why isn't this redirecting?  I put http://test.example.com in the browser, and it sometimes redirects and sometimes doesn't.  I've removed any .htaccess files just to be sure.  Thanks

 

<VirtualHost *:80>
    ServerName test.example.com
    DocumentRoot /var/www/concrete5/public
    <Directory "/var/www/concrete5/public">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
        RewriteEngine On
    </Directory>
RewriteCond %{SERVER_NAME} =test.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName test.example.com
    DocumentRoot /var/www/concrete5/public
    <Directory "/var/www/concrete5/public">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
        RewriteEngine On
    </Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/test.example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/test.example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/test.example.com/chain.pem
</VirtualHost>
</IfModule>

 

Share this post


Link to post
Share on other sites

Your RewriteCond is unnecessary.   %{SERVER_NAME} resolves to whatever the ServerName directive is, so that condition is always true.  %{HTTP_HOST} would be the name the request uses.  Unless this is your default vhost that should also always be equal to test.example.com though as you have no ServerAliases..

If you just want every non-https request to go to https, there is a simpler way:

<VirtualHost *:80>
        ServerName test.example.com

        Redirect permanent /  https://test.example.com
</VirtualHost>

This is what I use for pretty much all my sites.

Share this post


Link to post
Share on other sites
Posted (edited)

Thank you kicken,  While I think you are mostly correct, not sure if you are completely correct, but you definitely solved me problem!  For whatever reason, I just do not enjoy configuring web servers which makes me not very good at it which causes me mysterious issues such as this.  Instead, I too often copy and paste and assume things are correct.  For https, I use letsencrypt, and just execute sudo certbot --apache which automatically updates the http config file and creates the https config file.  After reading your post, I first confirmed that other sites which I do such for are correctly redirecting, and then I compared the conf files to this problem site.  For unknown reasons, this just happened to leave out the RewriteEngine on line, and after adding it, it works as expected.  Agree your solution is simpler and I believe it is the same as I previously was using before using letsencrypt.  Thanks again!

<VirtualHost *:80>
    ...
    <Directory "/var/www/concrete5/public">
        ...
    </Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =test.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

 

Edited by NotionCommotion

Share this post


Link to post
Share on other sites

You had RewriteEngine On, just inside your directory section instead of the VirtualHost.  Didn't think it would have mattered so didn't mention that.

7 hours ago, NotionCommotion said:

Agree your solution is simpler and I believe it is the same as I previously was using before using letsencrypt.

Not sure if your saying using Let's Encrypt made you change or not, but I use Let's Encrypt and cetbot just fine with the simple redirect.

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.