Http to Https Redirect Issue. Need some clarification

Viewed 59

I am trying to figure out why redirect http to https is not working in all cases. I have two domains on server. This domain has Virtual Host configuration. When I type www.example.com to browser server redirect me to https://www.example.com and also working with out www on to begin of the domain. However, when I type http://example.com redirect does not work. I think I try everything related to .htaccess file but nothing helps. When I comment all RewriteRule lines https redirect is also working as describe above. I am totally confused. Could anyone help? As you can see in the file I commented out the lines which are not working.

Delete this file

Here is my .htaccess file

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\.
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
#RewriteCond %{HTTP_HOST} .
#RewriteCond %{HTTP_HOST} !^www\.kayaon\.com
#RewriteRule (.*) http://www.kayaon.com/$1 [R=301,L]
#RewriteCond %{HTTP:X-Forwarded-Proto} =http 
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^(.*)$ index.html?url=$1 [QSA,L]
</IfModule>

Update it like this located in usr/local/apache/conf.d/vhosts

<IfModule mod_rewrite.c>
RewriteEngine On

# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{REQUEST_SCHEME} =http
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

# route everything to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.html?url=$1 [QSA,L]
</IfModule>

ssl.conf file located in usr/local/apache/conf.d/vhosts

<VirtualHost 185.86.155.150:8443>
    ServerName kayaon.com
    ServerAlias www.kayaon.com
    ServerAdmin webmaster@kayaon.com
    DocumentRoot /home/kvkkolay/kayaon
    UseCanonicalName Off
    ScriptAlias /cgi-bin/ /home/kvkkolay/kayaon/cgi-bin/

    #CustomLog /usr/local/apache/domlogs/kayaon.com.bytes bytes
    #CustomLog /usr/local/apache/domlogs/kayaon.com.log combined
    ErrorLog /usr/local/apache/domlogs/kayaon.com.error.log

    # Custom settings are loaded below this line (if any exist)
    # IncludeOptional "/usr/local/apache/conf/userdata/kvkkolay/kayaon.com/*.conf"

    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/kayaon.com.cert
    SSLCertificateKeyFile /etc/pki/tls/private/kayaon.com.key
    SSLCertificateChainFile /etc/pki/tls/certs/kayaon.com.bundle
    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown

  

    <IfModule mod_userdir.c>
        UserDir disabled
        UserDir enabled kvkkolay
    </IfModule>

    <IfModule mod_suexec.c>
        SuexecUserGroup kvkkolay kvkkolay
    </IfModule>

    <IfModule mod_suphp.c>
        suPHP_UserGroup kvkkolay kvkkolay
        suPHP_ConfigPath /home/kvkkolay
    </IfModule>

    <IfModule mod_ruid2.c>
        RMode config
        RUidGid kvkkolay kvkkolay
    </IfModule>

    <IfModule itk.c>
        AssignUserID kvkkolay kvkkolay
    </IfModule>

    <Directory "/home/kvkkolay/kayaon">
        Options -Indexes -FollowSymLinks +SymLinksIfOwnerMatch
        AllowOverride All Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch
        SSLRequireSSL
    </Directory>
  

</VirtualHost>

<VirtualHost 185.86.155.150:8443>
    ServerName webmail.kayaon.com

    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/kayaon.com.cert
    SSLCertificateKeyFile /etc/pki/tls/private/kayaon.com.key
    SSLCertificateChainFile /etc/pki/tls/certs/kayaon.com.bundle
    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown

    <IfModule mod_proxy.c>
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyVia Full
        ProxyPass / http://127.0.0.1:2095/
        ProxyPassReverse / http://127.0.0.1:2095/

        <Proxy *>
            AllowOverride All
        </Proxy>
    </IfModule>

    <IfModule mod_security2.c>
        SecRuleEngine Off
    </IfModule>

</VirtualHost>

<VirtualHost 185.86.155.150:8443>
    ServerName mail.kayaon.com

    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/kayaon.com.cert
    SSLCertificateKeyFile /etc/pki/tls/private/kayaon.com.key
    SSLCertificateChainFile /etc/pki/tls/certs/kayaon.com.bundle
    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown

    <IfModule mod_proxy.c>
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyVia Full
        ProxyPass / http://127.0.0.1:2095/
        ProxyPassReverse / http://127.0.0.1:2095/

        <Proxy *>
            AllowOverride All
        </Proxy>
    </IfModule>

    <IfModule mod_security2.c>
        SecRuleEngine Off
    </IfModule>

</VirtualHost>

<VirtualHost 185.86.155.150:8443>
    ServerName cpanel.kayaon.com

    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/kayaon.com.cert
    SSLCertificateKeyFile /etc/pki/tls/private/kayaon.com.key
    SSLCertificateChainFile /etc/pki/tls/certs/kayaon.com.bundle
    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown

    <IfModule mod_proxy.c>
        SSLProxyEngine on
        SSLProxyVerify none
        SSLProxyCheckPeerCN off
        SSLProxyCheckPeerName off
        SSLProxyCheckPeerExpire off
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyVia Full

        RewriteEngine on

        RewriteRule ^/roundcube$ /roundcube/ [R]
        ProxyPass /roundcube/ https://127.0.0.1:2031/roundcube/
        ProxyPassReverse /roundcube https://127.0.0.1:2031/roundcube/

        RewriteRule ^/pma$ /pma/ [R]
        ProxyPass /pma/ https://127.0.0.1:2031/pma/
        ProxyPassReverse /pma https://127.0.0.1:2031/pma/

        ProxyPass / https://127.0.0.1:2083/
        ProxyPassReverse / https://127.0.0.1:2083/

        <Proxy *>
            AllowOverride All
        </Proxy>
    </IfModule>

    <IfModule mod_security2.c>
        SecRuleEngine Off
    </IfModule>
    
</VirtualHost>

domianname.conf file

<VirtualHost 185.86.155.150:8181>
    ServerName kayaon.com
    ServerAlias www.kayaon.com
    ServerAdmin webmaster@kayaon.com
    DocumentRoot /home/kvkkolay/kayaon
    UseCanonicalName Off
    ScriptAlias /cgi-bin/ /home/kvkkolay/kayaon/cgi-bin/

    #CustomLog /usr/local/apache/domlogs/kayaon.com.bytes bytes
    #CustomLog /usr/local/apache/domlogs/kayaon.com.log combined
    ErrorLog /usr/local/apache/domlogs/kayaon.com.error.log

    # Custom settings are loaded below this line (if any exist)
    # IncludeOptional "/usr/local/apache/conf/userdata/kvkkolay/kayaon.com/*.conf"


    <IfModule mod_setenvif.c>
        SetEnvIf X-Forwarded-Proto "^https$" HTTPS=on
    </IfModule>

    <IfModule mod_userdir.c>
        UserDir disabled
        UserDir enabled kvkkolay
    </IfModule>

    <IfModule mod_suexec.c>
        SuexecUserGroup kvkkolay kvkkolay
    </IfModule>

    <IfModule mod_suphp.c>
        suPHP_UserGroup kvkkolay kvkkolay
        suPHP_ConfigPath /home/kvkkolay
    </IfModule>

    <IfModule mod_ruid2.c>
        RMode config
        RUidGid kvkkolay kvkkolay
    </IfModule>

    <IfModule itk.c>
        AssignUserID kvkkolay kvkkolay
    </IfModule>

    <Directory "/home/kvkkolay/kayaon">
        Options -Indexes -FollowSymLinks +SymLinksIfOwnerMatch
        AllowOverride All Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch
    </Directory>

</VirtualHost>

<VirtualHost 185.86.155.150:8181>
    ServerName webmail.kayaon.com

    <IfModule mod_proxy.c>
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyVia Full
        ProxyPass / http://127.0.0.1:2095/
        ProxyPassReverse / http://127.0.0.1:2095/

        <Proxy *>
            AllowOverride All
        </Proxy>
    </IfModule>

    <IfModule mod_security2.c>
        SecRuleEngine Off
    </IfModule>

</VirtualHost>

<VirtualHost 185.86.155.150:8181>
    ServerName mail.kayaon.com

    <IfModule mod_proxy.c>
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyVia Full
        ProxyPass / http://127.0.0.1:2095/
        ProxyPassReverse / http://127.0.0.1:2095/

        <Proxy *>
            AllowOverride All
        </Proxy>
    </IfModule>

    <IfModule mod_security2.c>
        SecRuleEngine Off
    </IfModule>

</VirtualHost>

<VirtualHost 185.86.155.150:8181>
    ServerName cpanel.kayaon.com

    <IfModule mod_proxy.c>
        <IfModule !ssl_module>
            LoadModule ssl_module modules/mod_ssl.so
        </IfModule>
        SSLProxyEngine on
        SSLProxyVerify none
        SSLProxyCheckPeerCN off
        SSLProxyCheckPeerName off
        SSLProxyCheckPeerExpire off
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyVia Full

        RewriteEngine on

        RewriteRule ^/roundcube$ /roundcube/ [R]
        ProxyPass /roundcube/ https://127.0.0.1:2031/roundcube/
        ProxyPassReverse /roundcube https://127.0.0.1:2031/roundcube/

        RewriteRule ^/pma$ /pma/ [R]
        ProxyPass /pma/ https://127.0.0.1:2031/pma/
        ProxyPassReverse /pma https://127.0.0.1:2031/pma/
        
        ProxyPass / https://127.0.0.1:2083/
        ProxyPassReverse / https://127.0.0.1:2083/

        <Proxy *>
            AllowOverride All
        </Proxy>
    </IfModule>

    <IfModule mod_security2.c>
        SecRuleEngine Off
    </IfModule>
    
</VirtualHost>

Edit 1

When I updated the .htaccess file as above I cannot access my site.

You don't have permission to access / on this server.

1 Answers

It seems your code is mixed of many rules lost it's intent.

Have it this way on Apache 2.4+:

RewriteEngine On

# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{REQUEST_SCHEME} =http
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

# route everything to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.html?url=$1 [QSA,L]
Related